microsoft / microsoft/TypeScript
strictNullCheck should detect unreachable default values and code after nullish operator
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
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.
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 damit, die beiden ausschlaggebenden Beispiele mit aktiviertem strictNullCheck zu reproduzieren: den Default-Wert x = 42 bei der Destrukturierung und x ??= 42. Bestimme die vorgesehenen Diagnosen für unerreichbare Default-Werte und Nullish-Zuweisungen. Verwende anschließend die vorhandene TypeScript-Compiler-Testsuite, um beide Fälle und ihr Verhalten im abgeschlossenen Zustand zu verifizieren.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 32/100