Quick Answer
TypeScript detects unreachable code path
Understanding the Issue
TypeScript's type checker can detect conditions that will always evaluate to false based on the types involved, helping catch logical errors.
The Problem
This code demonstrates the issue:
Typescript
Error
// Problem: Impossible condition
function process(x: string) {
if (x === null) { // Warning
console.log("null");
}
}
The Solution
Here's the corrected code:
Typescript
Fixed
// Solution 1: Correct the type
function process(x: string | null) {
if (x === null) { // Valid
console.log("null");
}
}
// Solution 2: Remove impossible check
function process(x: string) {
console.log(x.toUpperCase());
}
Key Takeaways
Review logic when TypeScript flags impossible conditions.