You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Spread operator in 'new' expressions support for ES5? #2369
The Array constructor is the %Array% intrinsic object and the initial value of the Array property of the global object. When called as a constructor it creates and initializes a new exotic Array object. When Array is called as a function rather than as a constructor, it also creates and initializes a new Array object. Thus the function call Array(…) is equivalent to the object creation expression new Array(…) with the same arguments.
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.
Activity
RyanCavanaugh commentedon Mar 17, 2015
Seems reasonable enough.
RyanCavanaugh commentedon May 4, 2015
Approved. Be sure to handle the case where the operand to
newis a computed expression that needs to be cached in a temporary variable.tinganho commentedon May 7, 2015
I'm almost done with a PR on this one. Though there are one issue with the type checker:
Isn't it correct TS code above? Why can't I do
new objectWithSpreadMethod.spreadMethodon a method?Shall I correct this behavior?
Playground:
http://www.typescriptlang.org/Playground#src=interface%20ObjectWithSpreadMethod%20%7B%0D%0A%20%20%20%20spreadMethod(x%3A%20number%2C%20y%3A%20number%2C%20...z%3A%20string%5B%5D)%3B%0D%0A%7D%0D%0A%0D%0Avar%20spread%3A%20string%5B%5D%3B%0D%0Avar%20objectWithSpreadMethod%3A%20ObjectWithSpreadMethod%3B%0D%0A%0D%0Anew%20objectWithSpreadMethod.spreadMethod(1%2C%202%2C%20%22string%22)%3B%0D%0Anew%20objectWithSpreadMethod.spreadMethod(1%2C%202%2C%20...spread)%3B%0D%0Anew%20objectWithSpreadMethod.spreadMethod(1%2C%202%2C%20...spread%2C%20%22string%22)%3B%20
RyanCavanaugh commentedon May 7, 2015
Seems like it should be typed like this:
Alternatively:
The code as written has
spreadMethodreturningany, which is not a valid invocation target fornew.tinganho commentedon May 7, 2015
Ryan Cavanaugh (@RyanCavanaugh) Ok thanks, I will land a PR later.
mhegazy commentedon May 18, 2015
thanks Tingan Ho (@tinganho)!
mdcone commentedon Aug 20, 2015
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.
tinganho commentedon Aug 20, 2015
mdcone could you share your classes? I tested it with TS classes and it seems to work for me.