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

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

Closed
thetrompf opened this issue Sep 11, 2017 · 4 comments
Closed

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

thetrompf opened this issue Sep 11, 2017 · 4 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@thetrompf
Copy link

thetrompf commented Sep 11, 2017

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'.
@DanielRosenwasser
Copy link
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,
}

@DanielRosenwasser DanielRosenwasser 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 Sep 11, 2017
@thetrompf
Copy link
Author

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

@mhegazy
Copy link
Contributor

mhegazy commented Sep 12, 2017

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)

@mhegazy mhegazy added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Sep 12, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Sep 26, 2017

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

@mhegazy mhegazy closed this as completed Sep 26, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

3 participants