-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Comments
The problem is that definite assignment doesn't take types into account, it just works on the control flow graph. Adding a |
Internally we use (simplified) function assertNever(value: never): never {
throw new Error("Illegal value: " + value);
} |
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 |
Yeah, that's why you have to write |
not I do use a similar helper in my code, which I called |
The function's never going to finish evaluating either way, so it doesn't really matter whether you |
Armchair perf speculation: |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
@andy-ms is this something you plan to tackle in the future? |
Consider the following code
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
The text was updated successfully, but these errors were encountered: