Skip to content

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

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'.

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