Skip to content

Enum with computed values, causes weird bug in union type exhaustion #18393

Closed

Description

TypeScript Version: 2.5.2

Working:

enum E {
    A = 2,
    B = 1  // <- constant value
}

function assertNever(obj: never, msg?: string): never {
    throw new Error(msg || `Unsupported option: ${obj}`);
}

function resolve(o: E) {
    switch (o) {
        case E.A: return 1;
        case E.B: return 2;
        default:
            assertNever(o);
    }
}

Not working:

enum E {
    A = 2,
    B = 2 << 1 // <- computed value
}

function assertNever(obj: never, msg?: string): never {
    throw new Error(msg || `Unsupported option: ${obj}`);
}

function resolve(o: E) {
    switch (o) {
        case E.A: return 1;
        case E.B: return 2;
        default:
            assertNever(o); // Argument of type 'E' is not assignable to parameter of type 'never'.
    }
}

Expected behavior:
The compiler should recognize that the types of o has been exhausted,
even though the enum has a bit shifted value.

Actual behavior:
Compile error:

Argument of type 'E' is not assignable to parameter of type 'never'.

Activity

  1. DanielRosenwasser commented on Sep 11, 2017

    @DanielRosenwasser
    Member

    Simpler case:

    enum E {
        A = 2,
        B = 2 << 1 // <- bit shifted value
    }
    
    function assertNever(obj: never, msg?: string): never {
        throw new Error(msg || `Unsupported option: ${obj}`);
    }
    
    function resolve(o: E) {
        switch (o) {
            case E.A: return 1;
            case E.B: return 2;
            default:
                assertNever(o); // Argument of type 'E' is not assignable to parameter of type 'never'.
        }
    }

    I think the problem may be that TypeScript is not diving into computed enum. For example, the following enum also triggers this:

    const enum E {
        A = +1,
        B = 2,
    }
  2. changed the title [-]Enum with bit-shifted values, causes weird bug in union type exhaustion[/-] [+]Enum with computed values, causes weird bug in union type exhaustion[/+] on Sep 11, 2017
  3. thetrompf commented on Sep 11, 2017

    @thetrompf
    Author

    Nice catch, I updated the issue description to basically use your case.

  4. mhegazy commented on Sep 12, 2017

    @mhegazy
    Contributor

    Under the covered there are really two kinds of enums, computed and union. This is needed to allow narrowing for enums/ treat them as unions, without breaking flag enums. having any expression in an enum initializer makes the enum behave as a flag enum (i.e. an open subset of the number domain) rather than a union enum (a collection of unit types)

  5. mhegazy commented on Sep 26, 2017

    @mhegazy
    Contributor

    Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

  6. locked and limited conversation to collaborators on Jun 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions