Skip to content

Missing warning on unreachable code #27637

Closed
@SimonGustavsson

Description

@SimonGustavsson

Sorry if this has been reported before, I tried my best searching for similar issues. I guess this is more of a "Is this 'As Designed'?" question as opposed to a bug report. It's caught me off guard a few times and thought I'd mention it at least.

TypeScript Version: 3.1.1

Search Terms: Type check, null, undefined

Code

// Example 1
const myNumber = 32;
if (myNumber === undefined) {
  console.log('This will never run');
}

// Example 2
const test: number | null = null;
if (test === undefined) {
  console.log('Woof');
} else {
  console.log('Meow');
}

Expected behavior:
A warning on line 2 in both examples that myNumber/test will never be undefined. (Or line 3 that the code is unreachable).

Actual behavior:
Code compiles fine without issue.

Playground Link: http://www.typescriptlang.org/play/#src=%2F%2F%20Example%201%0D%0Aconst%20myNumber%20%3A%20number%20%3D%2032%3B%0D%0Aif%20(myNumber%20%3D%3D%3D%20undefined)%20%7B%0D%0A%20%20console.log('This%20will%20never%20run')%3B%0D%0A%7D%0D%0A%0D%0A%2F%2F%20Example%202%0D%0Aconst%20test%20%3A%20number%20%7C%20null%20%3D%20null%3B%0D%0Aif%20(test%20%3D%3D%3D%20undefined)%20%7B%0D%0A%20%20console.log('Woof')%3B%0D%0A%7D%20else%20%7B%0D%0A%20%20console.log('Meow')%3B%0D%0A%7D

Related Issues: No

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions