IteratorResult<T> definition conflicts with the JS spec
See original GitHub issueTypeScript Version: 3.8.3
Search Terms: iterator return type
Code
const list = ['v1', 'v2', 'v3']
const f: () => Iterator<string> = () => {
let index = 0
return {
next: (): IteratorResult<string> => {
console.log(list)
if (index < list.length) {
return { value: list[index++]}
} else {
return { done: true }
}
}
}
}
Expected behavior: The code should compile without errors. Actual behavior: The code compiles with the error: “Type ‘{ done: true; }’ is not assignable to type ‘IteratorResult<string, any>’. Property ‘value’ is missing in type ‘{ done: true; }’ but required in type ‘IteratorReturnResult<any>’.(2322)”
The error can be bypassed by changing { done: true } to { value: undefined, done: true }. However, the specification allows omitting the value property. See Iteration Protocols
A suggested fix would be to change the definition of the type IteratorReturnResult to:
interface IteratorReturnResult<TReturn> {
done: true;
value?: TReturn;
}
Related Issues: #29982
Issue Analytics
- State:
- Created 3 years ago
- Comments:10 (4 by maintainers)
Top Results From Across the Web
ECMAScript® 2023 Language Specification - TC39
Introduction. This Ecma Standard defines the ECMAScript 2023 Language. It is the fourteenth edition of the ECMAScript Language Specification.
Read more >You Don't Know JS: ES6 & Beyond - GitHub Pages
IteratorResult value {property}: current iteration value or final return value ... is defined as sending a signal to an iterator that the consuming...
Read more >function* - JavaScript - MDN Web Docs - Mozilla
Generators are functions that can be exited and later re-entered. Their context (variable bindings) will be saved across re-entrances.
Read more >ECMAScript® 2022 Language Specification - Ecma International
4.4 Terms and Definitions. 4.5 Organization of This Specification. 5 Notational Conventions. 5.1 Syntactic and Lexical Grammars. 5.2 Algorithm Conventions.
Read more >javascript - js - avoiding namespace conflict - Stack Overflow
Properties of the object are the modules I defined before. // file BI.lib.js var lzheA = { foo: function() { } ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
fwiw I ran into this in practice when attempting to use a
ReadableStream
as an async interator. E.g. this example from Jake Archibald’s article doesn’t type correctly:I agree that
value
should be allowed to be removed whendone
istrue
, but only when the iterator is typed asIteratorResult<T, void>
, which is why I’m investigating treating a property of typevoid
as optional. However if your result is typedIteratorResult<T, number>
, then the only correct type for the return result is{ done: true, value: number }
. Makingvalue
optional for that case would remove type safety. However, if we allow properties of typevoid
to be optional as we do for parameters, then{ done: true }
would be assignable to{ done: true, value: void }
.We made it such that calling
generator.return()
also requires a value if your generator is typed with a non-void
return value, which ensures type safety. The only way I can see to accurately address this issue is to allowvoid
properties to be considered optional.