microsoft / microsoft/TypeScript
strictNullCheck should detect unreachable default values and code after nullish operator
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
Suggestion
Using strictNullCheck should allow detection of code that isn't supposed to be reached according to type analysis
🔍 Search Terms
strictNullCheck unreachble nullish default value
✅ Viability Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- 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.
Yes, especially "Statically identify constructs that are likely to be errors."
⭐ Suggestion
This assumes that it is desired to detect errors with unreachable code due to type analysis (see microsoft/TypeScript-Website#1604 ).
With strictNullCheck turned on, some code that is meant to be unreachable and thus an error is not flagged.
📃 Motivating Example
The = 42 below is unreachable (playground) and is the sign that some code is likely to be an error, but nothing is flagged
class Example {
foo({
x = 42
}: {
x: number
}) {
return x
}
}
Either the declaration should be changed to x?: number, or the default should be removed.
Another example (thanks to @jcalz) using the nullish operator.
function foo(x: number) { x ??= 42 }
Again, it would be helpful if an error or a warning would be raised as that code seems clearly to be a mistake.
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 riproducendo i due esempi motivanti con strictNullCheck abilitato: il valore predefinito x = 42 destrutturato e x ??= 42. Determina le diagnosi previste per i valori predefiniti irraggiungibili e le assegnazioni nullish, quindi usa la suite di test esistente del compilatore TypeScript per verificare entrambi i casi e il loro comportamento nello stato completato.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- 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
- 32/100