-
Notifications
You must be signed in to change notification settings - Fork 12.4k
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
TypeScript ignore expression if (object instanceof SomeClass)
inside for(){}
and inside ()=>{}
.
#13180
Comments
for (let element of arr) {
if (element instanceof ClassA) {
element.propA = true;
() => {
element.propA;
}
}
} Your inner function is closing over a mutable binding that is scoped outside the To make this work, use a function fn(arr: (ClassA | ClassB)[]) {
for(const element of arr) { |
@aluanhaddad, thanks for the answer. Mutable - possible, but in fact this code is no mutation. TypeScript checks so deeply? |
I think the question comes down to scope of the variable. A constant binding could not be scoped to the for of declarator. |
Also, CFA doesn't track what happens inside of closures to make inferences about types in the outer scope. My recommendation, and this is just my personal opinion, is that all bindings should be |
@aluanhaddad, thank you. |
TypeScript Version: 2.1.4
Code
Expected behavior:
Works without error
Actual behavior:
Throw error:
When I remove the loop
for(){}
. Works as expected:The text was updated successfully, but these errors were encountered: