'How to type a generator function with typscript

I would like to send a value back to the generator function using next which seems to complicate typing this function

function* gen(): IterableIterator<string> {
    const a = yield 'test';       // Line A
    console.log('received:' + a);
    yield a + '';
}

const x = gen();

console.log('main:' + x.next(99));  // Line B
console.log('main' + x.next());

DEMO

In my VSCODE I get the following error for Line A

Type 'undefined' is not assignable to type 'number'.

And In Stackblitz/demo I get an error for Line B

Argument of type '[99]' is not assignable to parameter of type '[] | [undefined]'.
  Type '[99]' is not assignable to type '[undefined]'.
    Type '99' is not assignable to type 'undefined'.(2345)

So my question is, how can I type the value I provide with next?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source