The issue arises from the fact that you are "overriding" the typical process of the `return` method by utilizing a `return` statement within a `finally` block, which discards the provided value and instead returns `result`. This inconsistency leads to a mismatch in signatures. In the usual scenario, the parameter value of the `return` method corresponds to the `TReturn` of the generator (example). As per MDN:
Generator.prototype.return()
Functions like a `return` statement inserted in the generator's body at its current suspended position, terminating the generator and enabling it to execute any necessary cleanup operations when used with a `try`...`finally` block.
(Cleanup tasks typically do not involve a `return` statement.)
If it helps, I suggest defining and using your generator slightly differently — simply utilize the result of `next`, without requiring that final `return`, as demonstrated below:
function *concatStrings(): Generator<string, void, string> {
// −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−^^^
let result = '';
// (Note the absence of `try`/`finally` since no clean up resources exist)
while (true) {
const data = yield result;
// −−−−−−−−−−−−−−−−−−−−−−−^^^^^^
result += data;
}
}
const g = concatStrings();
let r: IteratorResult<string, void>;
g.next();
r = g.next("a");
console.log(r.value); // "a"
r = g.next("b");
console.log(r.value); // "ab"
r = g.next("c");
console.log(r.value); // "abc"
Playground example
This approach also resolves the typing issue by adhering more closely to conventional practices. :-)