Skip to content

Type narrowing with non-literal const enums #19321

Closed
@jcludwig

Description

@jcludwig

Type narrowing does not appear to cover the cases where a const enum member's value is not a literal.

const enum ConstEnum {
    foo = 2 | 1,
}

function f(e: ConstEnum): number {
    switch (e) {
        case ConstEnum.foo:
            return 0;
        default:
            assertNever(e);
    }
}

export function assertNever(x: never): never {
    throw new Error('Expected never to have value ' + x);
}

This code will result in the following error: Argument of type 'ConstEnum' is not assignable to parameter of type 'never'. However, the switch should be covering all cases, and therefore I would expect the type of e to be never in the default case. Changing the value of foo to a literal by removing arithmetic results in the expected behavior (no error). It looks like the compiler is properly doing the arithmetic at compile-time to compute 2 | 1 = 3.

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