-
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
Variable is considered undefined in for-of loop when target is es6 #8357
Comments
Thanks, good catch. There's an interesting challenge in fixing this. We currently have the following definitions: interface IteratorResult<T> {
done: boolean;
value?: T;
}
interface Iterator<T> {
next(value?: any): IteratorResult<T>;
return?(value?: any): IteratorResult<T>;
throw?(e?: any): IteratorResult<T>;
} We derive the element type of an iterator from the type of the |
@mhegazy Opinions on this one? |
Thanks for the explanation! I think the proposed suggestions are good, but a little dangerous. A better fix would be, in my opinion, to change the type of IteratorResult to: type IteratorResult<TYield, TReturn> = { done: true, value: TYield } | { done: false, value: TReturn } A generator without a return statement could then be typed as |
as @ivogabe mentioned, the |
@mhegazy Sounds like a good solution for the meantime. |
I think that this also affects the spread operator in function calls: function sum(...xs: number[]) {
return xs.reduce((a,b) => a+b, 0);
}
const list = [1, 2, 3, 4, 5];
sum(...list);
// ~~~~~~~ Argument of type 'number | undefined' is not assignable to parameter of type 'number'.
// Type 'undefined' is not assignable to type 'number'. |
I'm wondering if it would be possible to use same solution as |
Fix #8357: Remove optionality for the definition of `IteratorResult`
do you have your copy of Also, a code snippet that can be copied/pasted would be much easier to use than screenshots. |
Please excuse, I think it's a problem with my setup anyway. I have an older JSPM install of typescript and even though I'm pointing VSCode to the correct NPM nightly libs it still seems to be picking up the JSPM install which is causing the errors shown. Sorry for the lack of info |
Treating this as a discriminated unions keyed on a bool would help, so it's not so bad to do something weaker for now if you think that solution will be coming soon: interface IteratorResult<T> {
done: true;
}
interface IteratorResult<T> {
done: false;
value: T;
} |
@evmar : You can't have an IteratorResult without a value, since you can return from a generator. But perhaps that should be strongly discouraged? |
The variable in a for-of loop contains
undefined
when the target is set to es6. For es5, everything works as expected.TypeScript Version:
nightly (1.9.0-dev.20160428)
Code
tsconfig.json:
Expected behavior:
Type of
x
isstring
in the loop body, no compile error.Actual behavior:
Type of
x
isstring | undefined
, which gives an error.@ahejlsberg
The text was updated successfully, but these errors were encountered: