Skip to content
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

Union types do not match narrowed overloads #46778

Closed
Haringat opened this issue Nov 11, 2021 · 2 comments
Closed

Union types do not match narrowed overloads #46778

Haringat opened this issue Nov 11, 2021 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@Haringat
Copy link

Bug Report

I am not sure if this is a bug or a design limitation, but when you have a function with narrowed overloads (see example) you cannot call the function with a broader union type although the union type does not go out of bounds of the overloads.

🔎 Search Terms

union types overloads

🕗 Version & Regression Information

  • I was unable to test this on prior versions because of time restraints

⏯ Playground Link

Simplified example

💻 Code

function isOneOrTwo(it: unknown): it is (1 | 2) {
    return it === 1 || it === 2;
}

function doSomething(it: unknown) {
    if (isOneOrTwo(it)) {
        return doSomethingElse(it);
    } else {
        throw new Error("out of range");
    }
}

function doSomethingElse(it: 1): "one";
function doSomethingElse(it: 2): "two";
function doSomethingElse(it: number) {
    switch (it) {
        case 1:
            return "one";
        case 2:
            return "two";
        default:
            throw new Error("out of range");
    }
}

🙁 Actual behavior

The compiler reports an error on the call to doSomethingElse because the union type is not assignable to either of the overloads.

🙂 Expected behavior

The function is callable and returns a union type of the relevant overload's return values.

@MartinJohns
Copy link
Contributor

Duplicate of #17471.

@andrewbranch andrewbranch added the Duplicate An existing issue was already created label Nov 11, 2021
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants