Skip to content

Single-element enums do not narrow to never #13182

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
krryan opened this issue Dec 27, 2016 · 2 comments
Closed

Single-element enums do not narrow to never #13182

krryan opened this issue Dec 27, 2016 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@krryan
Copy link

krryan commented Dec 27, 2016

TypeScript Version: 2.1.1 / nightly (2.2.0-dev.201xxxxx)

Code

In playground

const enum One {
    Foo = 1,
}

const enum Two {
    Foo = 1,
    Bar = 2
}

function neverFn(n: never) { }

function oneSwitch(one: One) {
    switch (one) {
        case One.Foo: break;
        default: neverFn(one);
    }
}

function twoSwitch(two: Two) {
    switch (two) {
        case Two.Foo: break;
        case Two.Bar: break;
        default: neverFn(two);
    }
}

function oneIfElse(one: One) {
    if (one === One.Foo) {

    }
    else {
        neverFn(one);
    }
}

function twoIfElse(two: Two) {
    if (two === Two.Foo) {

    }
    else if (two === Two.Bar) {

    }
    else {
        neverFn(two);
    }
}

Expected behavior:
one and two are both narrowed to never in the default case for their respective Switch functions, and in the else case for their respective IfElse functions, making them valid arguments to the neverFn call.

Actual behavior:
two narrows correctly, but the neverFn call in oneSwitch and oneIfElse has the error

Argument of type One is not assignable to parameter of type never.

@aluanhaddad
Copy link
Contributor

Aren't these contradictions in terms?

@normalser
Copy link

See #12771

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Dec 28, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants