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 from generator.next(injected)) has a known type interface. I've managed to enforce this contract by extending IterableIterator:
interfaceTypedIterableIterator<T,N>extendsIterableIterator<T>{next(value?: N): IteratorResult<T>;}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
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
yieldstatement (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: