Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type inference for generator yield statement #14883

Closed
ssynix opened this issue Mar 27, 2017 · 2 comments
Closed

Type inference for generator yield statement #14883

ssynix opened this issue Mar 27, 2017 · 2 comments

Comments

@ssynix
Copy link

ssynix commented Mar 27, 2017

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:

interface TypedIterableIterator<T, N> extends IterableIterator<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> {
    let injectedNum = yield num; // has inferred type `any`
}

function test() {
    let numYielder = 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.

{
    let injectedNum: number = yield num;
    // 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:

let returnValue = yield call(callback); // return value type of `callback` is lost
@yortus
Copy link
Contributor

yortus commented Mar 28, 2017

@ssynix this comment and this answer, both from #2983 may interest you. You might also want to check out the discussion in #2873.

@ssynix
Copy link
Author

ssynix commented Mar 28, 2017

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.

@ssynix ssynix closed this as completed Mar 28, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 21, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants