-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Should IteratorResult have optional value? #8938
Comments
That value was declared optional previously, but that indeed caused to much casts. See #8357. Ideally type IteratorResult<TYield, TReturn>
= { done: true, value: TYield }
| { done: false, value: TReturn } But that's not possible yet. Then you could define an iterator that does not return as |
As noted by @ivogabe we need to have boolean literal types, to be able to model this one accurately. |
I'm fairly new to TypeScript, so maybe this is obvious to others, but I was able to work around with: return function next(): IteratorResult<T> {
if (yielded < arr.length) {
return { value: arr[yielded++], done: false }
} else {
return ({ done: true } as IteratorResult<T>) // <= explicit cast here!
}
} |
the definition is currently not correct, the correct one would be: type IteratorResult<TYield, TReturn>
= { done: false, value: TYield }
| { done: done, value: TReturn | undefined } |
Should this be closed in favor of #2983? |
Yes. |
Looking at the ECMA spec for IteratorResult, it seems that
value
can be optional (whendone
istrue
)In the TypeScript definition, this would translate to:
This does create a potentially annoying situation in which, when `strictNullChecks
is true, the
value``has to be cast to``T`` (unless the new control-flow based type guards are smart enough to recognise this).As it is, however, it isn't possible to actually implement an
Iterator
withstrictNullChecks
because we can't return an undefined value forvalue
.What is the correct approach here?
The text was updated successfully, but these errors were encountered: