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

null comparison fails in switch statement with --strictNullChecks #8971

Closed
bmayen opened this issue Jun 5, 2016 · 2 comments
Closed

null comparison fails in switch statement with --strictNullChecks #8971

bmayen opened this issue Jun 5, 2016 · 2 comments
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@bmayen
Copy link
Contributor

bmayen commented Jun 5, 2016

TypeScript Version:

nightly (1.9.0-dev.20160604-1.0)

Code

let foo:number|null = 0;

switch(foo) {
  case null:
    break;
  case 0:
    console.log('bar');
}

Expected behavior:
Null check should be allowed

Actual behavior:
Compiler complains "TypeScript Type 'null' is not comparable to type 'number'. (TS2678)"

Note:
If I add an 'if' condition before the switch, the compiler does not complain about either the 'if' or the switch. For instance:

let foo:number|null = 0;

if (foo === null) { console.log('bar'); }

switch(foo) {
  case null:
    break;
  case 0:
    console.log('bar');
}
@ahejlsberg
Copy link
Member

In your first example the checker complains about the null case because control flow analysis has narrowed the type of foo to number and thus concluded (rightly so) that foo is can't be null. It is similar to the following:

let x: string | number;
x = "foo";  // x is known to be a string in the following
switch (x) {
    case 0:  // Error, x can't be number
    case 1:  // Error, x can't be number
    case "foo":
    case "bar":
}

Now, the thing that's confusing is that we allow the foo === null in the if statement in your second example. See #8452 for the reasoning on why that's permitted. We should probably extend that to switch statements as well.

The reason it works when you insert the if statement is that we take that statement as an assertion that x could be null at that point, even though control flow analysis has concluded it couldn't. See #8548 for the reasoning on that feature.

To sum up, the issue here is that we should always allow null and undefined cases in switch statements to be consistent with #8452.

@ahejlsberg ahejlsberg added the Bug A bug in TypeScript label Jun 5, 2016
@ahejlsberg ahejlsberg added this to the TypeScript 2.0 milestone Jun 5, 2016
@bmayen
Copy link
Contributor Author

bmayen commented Jun 5, 2016

Thanks for the explanation!

@ahejlsberg ahejlsberg added the Fixed A PR has been merged for this issue label Jun 7, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

3 participants