You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Unable to use done property of IteratorResult to narrow types in typescript 3.6.2 #33241
Expected behavior:
The code compiles. Checking the negation of result.done narrows the type of result.value to string.
Actual behavior:
iterator-result.ts(3,7): error TS2322: Type 'string | number' is not assignable to type 'string'.
Type 'number' is not assignable to type 'string'.
I thought the whole point of strictly typed iterators was that we could use the done to narrow the type of value, but it seems like a late-game change (making IteratorYieldResult.done optional) prevents us from doing this.
I’m fine with having result.done be optional for IteratorYieldResult but this raises the question why we didn’t also allow result.value to be optional in IteratorReturnResult, when it is idiomatic javascript to return { done: true } without a value in custom iterators.
Playground Link:
Typescript 3.6.2 is not available in the playground yet.
This is currently a limitation of control-flow when strictNullChecks is not enabled. The problem is that null and undefined are assignable to a type of true, therefore we cannot remove the done: true discriminant from the union in the negative case of !result.done. Please note that the following conditions do work:
Activity
MattiasBuelens commentedon Sep 11, 2019
According to this comment on #27059, this bug only happens when
strictNullChecksis off. If you can, you should enablestrictNullChecks(orstrict).brainkim commentedon Sep 11, 2019
Can confirm, was testing with a plain tsc call which did not enable strict.
rbuckton commentedon Oct 28, 2019
This is currently a limitation of control-flow when
strictNullChecksis not enabled. The problem is thatnullandundefinedare assignable to a type oftrue, therefore we cannot remove thedone: truediscriminant from the union in the negative case of!result.done. Please note that the following conditions do work: