Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Definite assignment for exhaustive switch statements. #20823

Closed
antialize opened this issue Dec 20, 2017 · 9 comments · Fixed by #32695
Closed

Definite assignment for exhaustive switch statements. #20823

antialize opened this issue Dec 20, 2017 · 9 comments · Fixed by #32695
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@antialize
Copy link

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

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Dec 20, 2017

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.

@DanielRosenwasser DanielRosenwasser added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Dec 20, 2017
@ghost
Copy link

ghost commented Dec 20, 2017

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

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

@antialize
Copy link
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
Copy link

ghost commented Dec 20, 2017

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

@Jessidhia
Copy link

not return assertNever(value)?

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

@ghost
Copy link

ghost commented Dec 22, 2017

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
Copy link
Member

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

@typescript-bot
Copy link
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
Copy link

methyl commented Jun 26, 2018

@andy-ms is this something you plan to tackle in the future?

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants