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

Missing warning on unreachable code #27637

Closed
SimonGustavsson opened this issue Oct 9, 2018 · 4 comments
Closed

Missing warning on unreachable code #27637

SimonGustavsson opened this issue Oct 9, 2018 · 4 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@SimonGustavsson
Copy link

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

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Oct 9, 2018
@RyanCavanaugh
Copy link
Member

Comparisons with undefined are always allowed in order to facilitate e.g. defensive parameter checks

@SimonGustavsson
Copy link
Author

That's a good point.

I guess there's no way to add a warning in my case where the types and values are well known without it getting messy/confusing for users?

@jack-williams
Copy link
Collaborator

Perhaps a related solution might be from: #27634

E.g

function isUndefined<T extends (undefined extends T ? any : never)>(value: T): value is T & undefined {
    return value === undefined;
}

// Example 1
const myNumber = 32;
if (isUndefined(myNumber)) {
  //            ^^^^^^^^ Error: argument of type number is not assignable to type never 
  console.log('This will never run');
}

The error is not in unreachable code, but a useless comparison.

@typescript-bot
Copy link
Collaborator

This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

4 participants