-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Spread operator in 'new' expressions support for ES5? #2369
Comments
Seems reasonable enough. |
Approved. Be sure to handle the case where the operand to |
I'm almost done with a PR on this one. Though there are one issue with the type checker: interface ObjectWithSpreadMethod {
spreadMethod(x: number, y: number, ...z: string[]);
}
var spread: string[];
var objectWithSpreadMethod: ObjectWithSpreadMethod;
new objectWithSpreadMethod.spreadMethod(1, 2, "string");
new objectWithSpreadMethod.spreadMethod(1, 2, ...spread);
new objectWithSpreadMethod.spreadMethod(1, 2, ...spread, "string"); Isn't it correct TS code above? Why can't I do Shall I correct this behavior? |
Seems like it should be typed like this: interface ObjectWithSpreadMethod {
spreadMethod: {
new (x: number, y: number, ...z: string[])
};
} Alternatively: interface ObjectWithSpreadMethod {
spreadMethod(x: number, y: number, ...z: string[]): void;
} The code as written has |
@RyanCavanaugh Ok thanks, I will land a PR later. |
thanks @tinganho! |
Does this seem to just be a happy coincidence with Array? Using this technique with my own TS classes doesn't work. Seems as if it really is impossible. According to MDN --
Further reading into the ES6 standard:
Which tells me that using something that does a [[Call]] with an array (.apply() or .call()) also has the same effect as a [[Construct]]. I'm fine with allowing this behavior in this case, so maybe allowing for a warning to be emitted instead of an error, or for special casing the errors to give a hint to the programmer as to why a program isn't behaving as expected might be a useful thing. |
@mdcone could you share your classes? I tested it with TS classes and it seems to work for me. |
I've found that #1931 supports this only for ES6, but it seems ES5 also can do this in any way.
This can be converted to ES5:
The text was updated successfully, but these errors were encountered: