Skip to content

Definite assignment for exhaustive switch statements. #20823

@antialize

Description

@antialize

Consider the following code

function test4(value: 1 | 2) {
    let x: string;
    switch (value) {
    case 1: x = "one"; break;
    case 2: x = "two"; break;
    }
    return x; //There is no path through the switch so x is alwayes assigned here.
}

Expected behavior:
The code should type check under --strict

Actual behavior:
Definite assignment does not realize that the switch is exhaustive and that all paths assign to x, so we get an error.

Checked with 2143a3f

Activity

DanielRosenwasser

DanielRosenwasser commented on Dec 20, 2017

@DanielRosenwasser
Member

The problem is that definite assignment doesn't take types into account, it just works on the control flow graph. Adding a default: throw new Error("..."); should do the trick though.

ghost

ghost commented on Dec 20, 2017

@ghost

Internally we use (simplified) default: throw assertNever(value); with

function assertNever(value: never): never {
    throw new Error("Illegal value: " + value);
}
antialize

antialize commented on Dec 20, 2017

@antialize
Author

I use that as well. Note however that

function test4(value: 1 | 2) {
    let x: string;
    switch (value) {
    case 1: x = "one"; break;
    case 2: x = "two"; break;
    default: assertNever(value); break;
    }
    return x; //There is no path through the switch so x is alwayes assigned here.
}

Will not work due to #20822

ghost

ghost commented on Dec 20, 2017

@ghost

Yeah, that's why you have to write throw assertNever(value); to indicate to control flow that it never returns.

Jessidhia

Jessidhia commented on Dec 22, 2017

@Jessidhia

not return assertNever(value)?

I do use a similar helper in my code, which I called unreachable.

ghost

ghost commented on Dec 22, 2017

@ghost

The function's never going to finish evaluating either way, so it doesn't really matter whether you return or throw the result -- just a style preference.

DanielRosenwasser

DanielRosenwasser commented on Dec 22, 2017

@DanielRosenwasser
Member

Armchair perf speculation: return probably won't trigger a de-opt.

typescript-bot

typescript-bot commented on Jan 6, 2018

@typescript-bot
Collaborator

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

methyl

methyl commented on Jun 26, 2018

@methyl

@Andy-MS is this something you plan to tackle in the future?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

      Participants

      @Jessidhia@antialize@methyl@DanielRosenwasser@typescript-bot

      Issue actions

        Definite assignment for exhaustive switch statements. · Issue #20823 · microsoft/TypeScript