-
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
Emit of 'arguments' in arrow functions is incorrect per ES6 spec. #1609
Comments
As a practical point, both FF and V8 (with function foo() { return () => arguments[0]; } foo(5)(6) // 6 (Unchanged in strict mode.) jstransform also does not capture the outer arguments object I think, but 6to5 does. The spec has finalized on this point for more than a year now, so perhaps it's okay to go ahead with this and expect ES6-supporting browsers to fix their implementation. But perhaps the ES6 emit could implement a warning for this? "Warning - using an unbound arguments inside this arrow function may not behave as you expect. Consider explicitly binding the outer arguments object, or use a splat on the arrow function." |
After discussing with @RyanCavanaugh, we've come to the decision that we should avoid changing the semantics of existing code and to give an error so as to push users away from using this construct. However, we will still emit so users will still be able to get this behavior if they desire. |
This probably deserves some documentation since I'm guessing this will hit a few people. What is the change?It is now an error to reference the implicit Why?The ECMAScript 6 specification says that no Prior to the ES6 compile target, the TypeScript compiler would emit an arrow function as a regular function, and access to the If ES3/ES5-targeted code that used Notably, several runtimes today do not respect this aspect of the ES6 spec and will incorrectly provide the inner function arguments as Our optionsWhen faced with this problem, we had a few choices: 1. Rewrite 2. Always downlevel-emit arrow functions. We could choose to not emit native arrow functions in ES6. This would preserve the non-compliant ES3/ES5 behavior where 3. Error today, maybe do option 1 later. This is where we've landed. Any TypeScript code that uses Once we're confident everyone has upgraded their code and sorted out the errors, and once the JavaScript runtimes start implementing the correct behavior, we can probably remove this error and start doing |
This issue has been cooperated into PR #1627 |
^ because I want this to show up in google search results ❤️ |
Hold on there, let's not forget the TypeScript diagnostic code, TS9002. |
I'm getting code TS2496 in TypeScript 1.5 alpha:
|
@robertpenner by design. Just use "function" instead of "()=>" |
Or use rest args:
Instead of
|
@basarat I get the same error even with
with error
But with TSC version 2.8.1. |
From 9.2.13 of the ES6 Spec Draft:
In fact, we should be capturing
arguments
from the first containing function expression and error if there is none. The correct emit for something likewould be the following:
The text was updated successfully, but these errors were encountered: