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
Union of Generator and AsyncGenerator introduces spurious undefined next() value path #42439
I was creating a coroutine-based interactive prompt library. It can consume either synchronous or asynchronous co-routines.
There doesn't seem to be any runtime issue associated with this approach, (in some configurations the library harmlessly awaits on a synchronous generator.next()).
However, any function which is declared as fulfilling a union of async+sync generators faces a compile error when a spurious undefined value is indicated as possible from a yield even though the next() call only accepts strings.
typeSyncSequenceFactory=()=>Generator<string,string,string>;typeAsyncSequenceFactory=()=>AsyncGenerator<string,string,string>;typeSequenceFactory=SyncSequenceFactory|AsyncSequenceFactoryconstsyncFactory: SyncSequenceFactory=function*(){letname="";while(!name){name=yield"What is your name?"}return`That's the end of the game, ${name}`}constasyncFactory: AsyncSequenceFactory=asyncfunction*(){letname="";while(!name){name=yield"What is your name?"}return`That's the end of the game, ${name}`}constlooserSyncFactory: SequenceFactory=function*(){letname="";while(!name){name=yield"What is your name?"}return`That's the end of the game, ${name}`}constlooserAsyncFactory: SequenceFactory=asyncfunction*(){letname="";while(!name){name=yield"What is your name?"}return`That's the end of the game, ${name}`}
🙁 Actual behavior
Compiler reports Type 'string | undefined' is not assignable to type 'string'.
🙂 Expected behavior
I don't believe this possible execution path is actually made possible by the union, since it is not possible in either of the unioned cases.
Activity