How can I get TypeScript to automatically infer the type of the result of a `yield` call? How can I get TypeScript to automatically infer the type of the result of a `yield` call? typescript typescript

How can I get TypeScript to automatically infer the type of the result of a `yield` call?


Unfortunately, Typescript just doesn't support this right now. And there doesn't really appear to be a good workaround other than simply putting a type annotation on every yield statement:

function* gen() {    let v: number = yield Promise.resolve(0);    return v;}

It's a tricky problem to solve, as the value returned by yield statements is entirely dependent on the semantics of whatever is consuming the generator. I know that yield Promise.resolve(0) will eventually return a number only because I know the semantics of coroutine, but v really could be anything.

But hopefully Typescript will add support for declaring these relationships as part of the type of the generator itself. Here's a relevant github issue.