microsoft / microsoft/TypeScript
Check truthy/falsy-ness for non-primitives to identify unreachable code
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.4k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
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.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit der Überprüfung der vorhandenen Diagnosen von TypeScript für unerreichbaren Code und Bedingungen und vergleiche anschließend deren Verhalten mit den Array- und Objektbeispielen im Issue. Lege fest, wie truthy/falsy Nicht-Primitive gemeldet werden sollten und ob die vorgeschlagene length-Anleitung angemessen ist; als erledigt gilt, wenn die Beispiele die beabsichtigten Diagnosen erzeugen, ohne das ausgegebene JavaScript zu ändern.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- javascript, typescript
- Bereich
- compilers
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 25/100