Unable to use done property of IteratorResult to narrow types in typescript 3.6.2 #33241
Labels
Design Limitation
Constraints of the existing architecture prevent this from being fixed
Working as Intended
The behavior described is the intended behavior; this is not a bug
Milestone
TypeScript Version: 3.6.2
Search Terms:
async iterator IteratorResult iterable next return throw type narrowing
Code
file: iterator-result.ts
Expected behavior:
The code compiles. Checking the negation of
result.done
narrows the type ofresult.value
tostring
.Actual behavior:
I thought the whole point of strictly typed iterators was that we could use the
done
to narrow the type ofvalue
, but it seems like a late-game change (makingIteratorYieldResult.done
optional) prevents us from doing this.I’m fine with having
result.done
be optional forIteratorYieldResult
but this raises the question why we didn’t also allowresult.value
to be optional inIteratorReturnResult
, 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.
Related Issues:
#30790 <- pr which introduced this change.
#2983
#10564
#11375
#27059
The text was updated successfully, but these errors were encountered: