Skip to content

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

Closed
@bmayen

Description

@bmayen

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');
}

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions