microsoft / microsoft/TypeScript
Check truthy/falsy-ness for non-primitives to identify unreachable code
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.4k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
Suggestion
In some cases TypeScript can identify unreachable code and throws an error:
const isTrue = true;
if (isTrue === false) {
console.log('Can never be reached.')
}
In other cases it is currently not possible, but it would be very helpful. (Maybe even with a suggestion what could have been meant - see array example.)
const isArray = ['hello'];
if (!isArray) {
console.log('Can never be reached. You probably meant `if (!isArray.length) {`.')
}
const isObject = {};
if (!isObject) {
console.log('Can never be reached.')
}
🔍 Search Terms
- reachable / unreachable
- dead code
- condition
- falsy / truthy
- primitive / non-primitive
- always
✅ Viability Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript code -> It can break code, but only to show potential bugs.
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
⭐ Suggestion
Throw errors on unreachable code examples.
const isArray = ['hello'];
if (!isArray) { // error: This condition will always return false. Did you meant to check `isArray.length`?
console.log('Can never be reached. You probably meant `if (!isArray.length) {`.')
}
const isObject = {};
if (!isObject) { // error: This condition will always return false.
console.log('Can never be reached.')
}
// OR:
if (isArray) {
} else {
console.log('Can never be reached. You probably meant `if (!isArray.length) {`.')
}
📃 Motivating Example
This week I saw a colleague which could have found a bug way sooner by introducing such a check. ❤️
💻 Use Cases
Finding bugs.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing TypeScript's existing unreachable-code and condition diagnostics, then compare their behavior with the array and object examples in the issue. Define how truthy/falsy non-primitives should be reported and whether the suggested length guidance is appropriate; done means the examples produce the intended diagnostics without changing emitted JavaScript.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, typescript
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100