Skip to content

strictNullChecks false positive when all possible conditions have been defined #27909

@Ky6uk

Description

@Ky6uk

TypeScript Version: v3.1.3

Search Terms:
strictNullChecks, switch, case, condition, if, variable is used before being assigned.

Code

const enum Test {
  A, B
}

function test(x: Test) {
  let bork: string;

  switch (x) {
    case Test.A: {
      bork = 'a';

      break;
    }

    case Test.B: {
      bork = 'b';

      break;
    }
  }
  
  return bork;
  //     ^^^^
  //     Variable 'bork' is used before being assigned.
}

Expected behavior:
No errors here.

TS should know about all possible checks. Right now we can get some kind of error: Not all code paths return a value. in cases when we didn't define all possible cases or when default has been omited.

Actual behavior:
We got an error Variable is used before being assigned. It also happens with if-else-if variant.

Playground Link:
Click Me!

Related Issues:
Possible related issue: #27239. But this one is out of the loop.
Also: #9655, #10470, #17358, #20409, #23271.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions