-
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
Spread operator is not correctly translated into JS #8856
Comments
My target is ES5 so not sure if this is related, but instances of
But the TypeScript compiler converts |
Perhaps the emit should use |
I've found that |
Regarding The spread operator on So the compiler emits a call to |
@Arnavion, if the compiler had spit out an error, I wouldn't have researched for a GH issue or a work-around as it would have been clear what the problem was. I didn't get an error until the code was actually executed in my application. Whether or not this is due to some TSC setting, I don't know, I just know it's an issue in our environment. |
Of course that depends on how @icfantv acquired the types for interface Set<T> extends Array<T> {
}
interface SetConstructor {
new <T>(): Set<T>;
}
declare var Set: SetConstructor;
[...new Set()]; // no errors |
@kitsonk excellent point. I am upgrading an Angular 1 app to Angular 2 and have to manage the typings myself to some extent and am at the mercy of those typings. While I would like to believe that they are solid, I have run into issues as noted above where the compiler will not complain but the generated code is not valid. |
The compiler has no way to know if a definition is correct or not. if the definition says a If you are using |
@mhegazy, can you please elaborate on what you mean when you say the compiler has no way of knowing if a definition is correct? If I say |
It is correct. When figuring out how to down emit something, the compiler has to understand what it can and cannot do. When looking at the spread operator and a target of ES5, it says to itself "I can spread things that look like Arrays". The only way it knows if something is an Array or not is using the types it can reason out from the code. If it is passed something that allegedly is an Array, it will do what its down emit code says, which is to do a How else could it be done? |
@mhegazy I feel the issue got side-tracked. Can you reopen for the original issue? |
+1 for this original issue -- a straightforward case, I think? |
I ran into this issue today writing some JSX using the spread operator; the way that TS handles the simplest spread case is strange! Here's a specific StackOverflow answer recommending |
nvm, see @Arnavion's reply below |
@brettjurgens This issue is about spread operator downlevel emit for arrays with holes. Your issue is #2696 |
Spread operator doesn't play well with iterators. |
Is it going to be fixed? I mean hey, it's almost 3 years old. |
I am not certain there is anything actionable here. We previously decided that TypeScript's default ES5 transpile for spread and for..of (which forgo precise runtime semantics over runtime performance) would remain as-is, and introduced |
* Change target to es2015 to support ie11 * Change tsconfig build target to es5 * Add downlevelIteration to compilerOptions to support ES5. Read more here: microsoft/TypeScript#8856
I will consult a friend to verify that I am not missing something obvious. Apparently it was probably a result of microsoft/TypeScript#8856
I will consult a friend to verify that I am not missing something obvious. Apparently it was probably a result of microsoft/TypeScript#8856
TypeScript Version: 1.8
Target: ES5
The following code:
translates into:
However, the ES6 meaning is not the same. See the output below:
Expected behavior:
The text was updated successfully, but these errors were encountered: