-
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
Super constructor spread #11679
Comments
I think the compiler is correct to flag the example as non-type-safe. You could instantiate You'd get the same error if you wrote |
If the constructor of A can take an undetermined number of arguments, then it would make sense to use class A {
values: any[];
constructor(...args: any[]) {
this.values = args;
}
}
class B extends A {
constructor(...args: any[]) {
super(args);
}
} However, in this case it looks like the number of arguments of A is determined. So it would be better to do the following: class A {
constructor(a: number, b: string) {}
}
class B extends A {
constructor(...args: any[]) {
if (args.length < 2) throw new Error();
let a: number = args[0];
let b: string = args[1];
super(a, b);
}
} If the number of constructor arguments in B can be determined, then I would recommend to avoid using class A {
constructor(a: number, b: string) {}
}
class B extends A {
constructor(a: number, b: string) {
super(a, b);
}
} The |
For the record, if you just didn't write a constructor at all, TypeScript would just "inherit" the constructor. See here. |
TypeScript Version: 2.0.3
Code
Expected behavior:
It's possible to pass arguments to super constructor without specifying them explicitly.
E.g. when extending classes of external framework, like React, which calls constructors.
Actual behavior:
Error: Supplied parameters do not match any signature of call target
The text was updated successfully, but these errors were encountered: