Skip to content

narrowing in switches doesnt work on constrained unions #20375

Closed
@zpdDG4gta8XKpMCd

Description

@zpdDG4gta8XKpMCd

assuming my understanding it right: an extended union is a subset (subtype) of the original union

    declare function never(never: never): never;
    type X = 'A' | 'B' | 'C';
    void function fn<Y extends X>(y: Y): Y {
        switch (y) {
            case 'A': return y;
            case 'B': return y;
            case 'C': return y;
            default: return never(y); // <-- y expected to be never, actual Y
        }
    }

same problem with objects

    type X = { k: 'A' } | { k: 'B' } | { k: 'C' };
    void function fn<Y extends X>(y: Y): Y {
        switch (y.k) {
            case 'A': return y;
            case 'B': return y;
            case 'C': return y;
            default: return never(y); // <-- y expected to be never, actual Y
        }
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions