-
Notifications
You must be signed in to change notification settings - Fork 12.8k
super() with an options object (possibly a FAQ) #7644
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
The assignment to You could assign to a temporary (eg back to constructor(x: Something, y: SomethingElse, options: any = {}) {
options = $.extend({}, defaults, options);
super(options.blah);
this.options = options;
} ... but that still hits the error that the I've worked around a similar issue (needed to swap derived class constructor parameters before calling base class constructor to maintain backward compatibility) by abusing an IIFE as the constructor(x: Something, y: SomethingElse, options: any = {}) {
super((() => {
options = $.extend({}, defaults, options);
return options.blah;
})());
this.options = options;
} Edit: Since you're always assigning to |
The problem is that all of these are ugly hacks, and it looks very strange that the usual facility for keyword arguments thing cannot work in such constructors. BTW, I couldn't get your hack to work, perhaps the TS version I'm using is too old. But if we're talking about such workarounds, then here's another ugly hack that works around it (based on an old Lisp hack of abusing optional arguments similarly):
In any case, I find it surprising that there is no proper way to do this, given that it's a common enough JS pattern that it's been embraced as the proper way to do keyword arguments. |
Not 100% sure what the best approach is, but since this is really an ES6 thing (must invoke |
@RyanCavanaugh, so I looked at it more, and I still think that there's a specific TS issue here. I can understand where the ES6 restriction is coming from, but trying it via TS still seem weird. I dug some more, and it looks like usually initialized properties are handled after the
This works fine, and the output has As a side note, I also think that it'll be good to add it to your FAQ since people will probably do what I did, and look for the error message. So a quick entry saying that it's an ES6 issue would be better than nothing... |
@elibarzilay I did say
|
@Arnavion, see the example in my reply to @RyanCavanaugh above: the problem is that if there is any initialized member TS does the assignment before the |
(Apologies if this is known, but I search a bunch of places and didn't find anything.)
Is there a good way to deal with a constructor of a derived class that is getting an options kind of value that needs to be merged with a defaults value? I started with something like this:
This failed and I eventually resorted to:
which is duplicating functionality (and sloppy).
Hopefully I'm not the only one who ran into this problem and there's good way to do that?
The text was updated successfully, but these errors were encountered: