-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Generators/Async Generators with TReturn type argument which extends undefined have a return method with a non-optional argument in 3.6.2 #33357
Comments
return
method with a non-optional argument in 3.6.2
If a generator indicates it returns We currently allow you to call |
Did some more playing around. Replacing function *gen(): Generator<number, number | void> {
yield 1;
return 2;
}
gen().return(); Was this intentional? This solution makes sense to me, because the TReturn type describes the return value of the generator type and the |
If the optional-argument behavior of
This feels like a valid use-case for void parameters. Related issues: #29131 |
Yes, the behavior of |
Using function* createGen(): Generator<number> {
yield 1;
yield 2;
}
const gen = createGen();
console.log(gen.next()); // {value: 1, done: false}
gen.return(); // TReturn is any but return still expects an argument. If the return type of the generator is function* createGen(): Generator<number, void> {
yield 1;
yield 2;
}
const gen = createGen();
console.log(Math.max(gen.next().value!, 0)); // non-null assertion won’t eliminate void I stand by my comments on the original PR that |
@rbuckton |
Related issue about making undefined parameters optional: |
TypeScript Version: 3.6.2
Search Terms:
generator iterator return argument optional required
Code
Expected behavior:
The argument passed to the
return
method should be optional. Not sure if this is possible with typescript.Actual behavior:
Playground Link:
Playground is not available for 3.6.2
Related Issues:
#30790 Strongly typed iterator PR.
#32682 Other (async) iterator usability issues.
#33239 Trouble implementing async iterator interface in 3.6.2
#33241 Unable to use done property of IteratorResult to narrow types in typescript 3.6.2
The text was updated successfully, but these errors were encountered: