Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Exhaustiveness checking current state #1304

@igoradamenko

Description

@igoradamenko

Hey there!

I'm reading Handbook's “Advanced types” chapter and there's a section describing exhaustiveness checking which says (as I understand it) that this code shouldn't warn us at all about missed case in area:

interface Square {
    kind: "square";
    size: number;
}

interface Rectangle {
    kind: "rectangle";
    width: number;
    height: number;
}

interface Circle {
    kind: "circle";
    radius: number;
}

interface Triangle {
    kind: "triangle";
    a: number;
    b: number;
    c: number;
}

type Shape = Square | Rectangle | Circle | Triangle;

function area(s: Shape) {
    switch (s.kind) {
        case "square": return s.size * s.size;
        case "rectangle": return s.height * s.width;
        case "circle": return Math.PI * s.radius ** 2;
    }
}

But it does. As I see it works because of the noImplicitReturns flag which emits:

Not all code paths return a value. (7030)

And it disappears when we change the code into this:

function area(s: Shape) {
    switch (s.kind) {
        case "square": return s.size * s.size;
        case "rectangle": return s.height * s.width;
        case "circle": return Math.PI * s.radius ** 2;
        case "triangle": return 0; // let's pretend here's Heron's formula
    }

So I guess that the example should be a bit more complicated to describe the problem well.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions