Current definition of IteratorResult demands value to be provided regardless of whether done is true or false.
Suggestion: Change IteratorResult to something like this
type IteratorResult<T> =
{ readonly done: true } |
{ readonly done: false, readonly value: T }
I am currently trying to build a custom async iterator that cannot be done with async generator, it is absurd that I had to provide some value when iteration is done.
My suggestion meets these guidelines:
I think I just force cast to as any or sth.
Edit:
Found in my code, lol
//We passed the end of the last page
return {
done : true,
value : undefined as any,
};
@AnyhowStep Yeah, that was my workaround too. But I think it shouldn't be, hence the request.
duplicate of #11375?
According to the spec and MDN the value property may (but is not required to) be present when done is true.
I think the proper typing should be
type IteratorResult<T> =
{ readonly done: true, readonly value?: T } |
{ readonly done: false, readonly value: T }
or even:
type IteratorResult<T> =
{ readonly done: true, readonly value?: any } |
{ readonly done: false, readonly value: T }
@rbuckton think we've got enough infrastructure in place to be able to try this lib change yet?
@hwanders If we were to follow the spec, done should be optional:
type IteratorResult<T> =
{ readonly done: true, readonly value?: T } |
{ readonly done?: false, readonly value: T }
For reference, @KSXGitHub is talking about this: https://tc39.github.io/ecma262/#sec-iteratorresult-interface (emphasis added):
If the end of the iterator was reached
doneis true. If the end was not reacheddoneis false and a value is available. _If adoneproperty (either own or inherited) does not exist, it is consider to have the value false._
And this (emphasis added):
If
doneis false, this is the current iteration element value. Ifdoneis true, this is the return value of the iterator, if it supplied one. If the iterator does not have a return value,valueis undefined. In that case, _thevalueproperty may be absent from the conforming object if it does not inherit an explicitvalueproperty._
There is, however, nothing that indicates done or value are read-only.
@rbuckton Although the spec does not _demand_ the properties to be either writable or read-only, not making them read-only makes it impossible to use a read-only object (e.g. frozen) as an IteratorResult. The way I see it, you are not supposed to modify these properties unless stated otherwise.
This issue has been marked as a duplicate and has seen no activity in the last day. It has been closed automatic house-keeping purposes.
Most helpful comment
duplicate of #11375?