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
I've just started with Typescript and ES6/ES7 features and have run into the following case where I'm unable to enforce type-safety with generators.
I'm using generators in a context where the return value of the yield statement (the injected value from generator.next(injected)) has a known type interface. I've managed to enforce this contract by extending IterableIterator:
However, Typescript is still unable to narrow down the type of injectedNum.
function*yieldNumber(num: number): TypedIterableIterator<number,number>{letinjectedNum=yieldnum;// has inferred type `any`}functiontest(){letnumYielder=yieldNumber(2);numYielder.next('give me type-safety');// successfully fails to compile}
The only workaround I've found so far is to manually annotate the injected value and cross my fingers that I didn't use the wrong type.
{letinjectedNum: number=yieldnum;// type-safe from here on... almost}
This is a very simple use case, but my actual use case is with redux-saga, which heavily uses generators. Example code:
letreturnValue=yieldcall(callback);// return value type of `callback` is lost
The text was updated successfully, but these errors were encountered:
Thanks for the links! I like the term "generator return type propagation". Couldn't figure out the right set of keywords to look up this topic earlier.
I'm going to close this since it's clearly a duplicate of what's being discussed in the mentioned links.
TypeScript Version: 2.2.1
I've just started with Typescript and ES6/ES7 features and have run into the following case where I'm unable to enforce type-safety with generators.
I'm using generators in a context where the return value of the
yield
statement (the injected value fromgenerator.next(injected)
) has a known type interface. I've managed to enforce this contract by extendingIterableIterator
:However, Typescript is still unable to narrow down the type of
injectedNum
.The only workaround I've found so far is to manually annotate the injected value and cross my fingers that I didn't use the wrong type.
This is a very simple use case, but my actual use case is with
redux-saga
, which heavily uses generators. Example code:The text was updated successfully, but these errors were encountered: