microsoft / microsoft/TypeScript
Check truthy/falsy-ness for non-primitives to identify unreachable code
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.4k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
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.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia esaminando le diagnostiche esistenti di TypeScript per il codice irraggiungibile e le condizioni, quindi confrontane il comportamento con gli esempi di array e oggetti nell’issue. Definisci come dovrebbero essere segnalati i non primitivi truthy/falsy e se le indicazioni suggerite su length siano appropriate; il lavoro è completato quando gli esempi producono le diagnostiche previste senza modificare il JavaScript emesso.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- javascript, typescript
- Ambito
- compilers
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 25/100