Skip to content

Incorrect discriminated union narrowing following exhaustive if/else #9246

Closed
@DanielRosenwasser

Description

@DanielRosenwasser
type Shape = { kind: "Circle"; radius: number }
           | { kind: "Rectangle"; width: number; height: number }


function area(s: Shape) {
    if (s.kind === "Circle") {
        return s.radius ** 2;
    }
    else if (s.kind === "Rectangle") {
        return s.width * s.height;
    }
    return assertNever(s);
}

function assertNever(x: never): never {
    return x;
}

Expected: No error
Actual:

test.ts(12,24): error TS2345: Argument of type '{ kind: "Circle"; radius: number; }' is not assignable to parameter of type 'never'.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.SuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions