-
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
for...of with strictNullChecks #9631
Comments
+1 Example from our codebase: function frob(changes: ChangeRecord[]) {
for (const change of changes) {
// TS 2.0 infers change: ChangeRecord | undefined
// All uses of `change` are correct but create a "Object is possibly 'undefined' error
}
} |
Just noticed that the problem also exists for the spread operator: const source: number[] = [1,2,3];
const target: number[] = [];
target.push(...source); // error TS2345: Argument of type 'number | undefined' is not assignable to parameter of type 'number' |
If you are using the version of the library that ships with the compiler, you shouldn't see this behavior see #8357. The behavior you are seeing is caused by a stale declaration of |
Yup, that was the issue. Thanks, @mhegazy. |
Ha... I switched to the new TS modular libs after making sure the code compiled. I see now that the issue went away, indeed. |
thanks for verifying. closing. |
I was able to dig up a lot of discussion about why
myArray[i]
should not be a nullable type, and that makes sense. But I wasn't able to dig up anything onfor ... of
loops.How come the following:
makes
y
a nullable type? All the arguments sayingmyArray[i]
shouldn't be a nullable type also seem to apply tofor ... of
loops, as far as I can see.The text was updated successfully, but these errors were encountered: