Skip to content

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

Closed
birtles opened this issue Jun 26, 2020 · 5 comments
Closed

Generator functions cannot be overloaded #39274

birtles opened this issue Jun 26, 2020 · 5 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@birtles
Copy link

birtles commented Jun 26, 2020

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

// NOT using generators
function yer(type: 'a'): Array<number>;
function yer(type: 'b'): Array<string>;

function yer(type: 'a' | 'b'): Array<any> {
    if (type === 'a') {
        return [0, 1, 2];
    }
    return ['hello', 'there'];
}

// Using generators
function* bleh(type: 'a'): IterableIterator<number>;
function* bleh(type: 'b'): IterableIterator<string>;

function* bleh(type: 'a' | 'b'): IterableIterator<any> {
    if (type === 'a') {
        yield 0;
        yield 1;
        yield 2;
        return;
    }

    yield 'hello';
    yield 'there';
}
Output
"use strict";
function yer(type) {
    if (type === 'a') {
        return [0, 1, 2];
    }
    return ['hello', 'there'];
}
function* bleh(type) {
    if (type === 'a') {
        yield 0;
        yield 1;
        yield 2;
        return;
    }
    yield 'hello';
    yield 'there';
}
Compiler Options
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "useDefineForClassFields": false,
    "alwaysStrict": true,
    "allowUnreachableCode": false,
    "allowUnusedLabels": false,
    "downlevelIteration": false,
    "noEmitHelpers": false,
    "noLib": false,
    "noStrictGenericChecks": false,
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "esModuleInterop": true,
    "preserveConstEnums": false,
    "removeComments": false,
    "skipLibCheck": false,
    "checkJs": false,
    "allowJs": false,
    "declaration": true,
    "experimentalDecorators": false,
    "emitDecoratorMetadata": false,
    "target": "ES2017",
    "module": "ESNext"
  }
}

Playground Link: Provided

@ilogico
Copy link

ilogico commented Jun 26, 2020

As far as I can tell, generator functions don't have a representation in the type system.
They are functions that return generators, just like async functions are merely functions that return promises.

That being said, you can overload them normally. Just remove the * like this.

@birtles
Copy link
Author

birtles commented Jun 26, 2020

That being said, you can overload them normally. Just remove the * like this.

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';
}

@birtles
Copy link
Author

birtles commented Jun 26, 2020

Is there a way to do the same for async generators?

Nevermind, I realized I can overload this by removing both async and *:

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';
}

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Jun 26, 2020
@typescript-bot
Copy link
Collaborator

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.

@AgainPsychoX
Copy link

AgainPsychoX commented Aug 14, 2022

Why can't we allow sugar syntax? I wish it would work with both star and async...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

5 participants