-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Generator functions cannot be overloaded #39274
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
Comments
As far as I can tell, generator functions don't have a representation in the type system. That being said, you can overload them normally. Just remove the |
Oh, interesting! Is there a way to do the same for async generators? The specific code I am trying to write is as follows: async function* bleh(type: 'a'): AsyncIterableIterator<number>;
async function* bleh(type: 'b'): AsyncIterableIterator<string>;
async function* bleh(type: 'a' | 'b'): AsyncIterableIterator<any> {
if (type === 'a') {
yield 0;
yield 1;
yield 2;
return;
}
yield 'hello';
yield 'there';
} |
Nevermind, I realized I can overload this by removing both function bleh(type: 'a'): AsyncIterableIterator<number>;
function bleh(type: 'b'): AsyncIterableIterator<string>;
async function* bleh(type: 'a' | 'b'): AsyncIterableIterator<any> {
if (type === 'a') {
yield 0;
yield 1;
yield 2;
return;
}
yield 'hello';
yield 'there';
} |
This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow. |
Why can't we allow sugar syntax? I wish it would work with both star and async... |
TypeScript Version: 3.9.2
Search Terms: overload generator
Expected behavior: It should be possible to overload generators (including async generators) just as it is possible to overload functions.
Actual behavior:
TS1222 An overload signature cannot be declared as a generator
is returned.Related Issues: #25710
Code
Output
Compiler Options
Playground Link: Provided
The text was updated successfully, but these errors were encountered: