Skip to content

Definite assignment for exhaustive switch statements. #20823

Closed
@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

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