-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Support super.apply from child constructor #1790
Comments
This seems like a reasonable variant to support |
Is the spread operator enough to support this scenario? |
Yes 🌹 |
Was this closed as "the spread operator will work here sometime in the future" or "the spread operator will work here now"? I just using the spread operator in the super call and I get Edit: To clarify, I'm using the nightly. |
@jbrantly can you provide more information, this works today: class Parent {
constructor(...rest) {
}
}
class Child extends Parent {
constructor(...args) {
super(...args);
}
} |
Sorry, I should have been much more specific. I couldn't exactly duplicate the issue I was seeing earlier, but my question boils down to mimicking the class Parent {
constructor(foo?: string, bar?: number) {
}
}
class Child extends Parent {
constructor(foo?: string, bar?: number) {
super(...arguments); // this currently fails
}
} So, for my purposes, I care less about rest parameters than I do about a shorthand to just pass all parameters. |
@jbrantly, this is a separate issue. can you file another one for this. |
Yea, I was thinking the same thing. Will do. |
Would you reconsider to reopen this issue? |
What are you doing where the spread operator isn't sufficient? |
@RyanCavanaugh it is possible to use spread operator, but it feels of a "boilerplate" that doesn't make code so elegant. for example, React and // You can do:
class SomeComponent extends React.Component<IProps, IState> {
constructor(...args) {
// I want to keep everything which is future related like unstable `context`
// without the need to re-factor
super(...args);
// The way to extract props
const [props] = args;
// do something with props;
}
}
export default someThingThatInhiritAndModifyComponent(SomeComponent);
// But this is way more elegant:
class SomeComponent extends React.Component<IProps, IState> {
constructor(props) {
super(...arguments);
// do something with props;
}
}
// And a bonus, a way which is more elegant to describe a signature
class A extends B {
constructor(x: number, y: number) {
super(...arguments);
// ···
}
} of corse i might be wrong, but this is my view |
I'm confused why this issue has not received much attention (in terms of thumbs up or comments). Particularly the scenario with subclassing Just to clarify, this does not work:
|
TypeScript is really stingy about the way you call
super
from the child constructor. I have seen questions on SO e.g. this one, where people want to splice and dice arguments they receive from varargs (rest parameters) before passing it on to super. My workarounds have been ugly:Suggestion: Support
super.apply(this, /*any|arr[]*/)
as a part of the language grammar.Also tagging @Steve-Fenton as he's answered similarly in the past.
The text was updated successfully, but these errors were encountered: