microsoft / microsoft/TypeScript
Warn/error when testing a non-nullable type for null
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 2 T. 4 Std.
- Gemergte PRs (30 T.)
- 132
Beschreibung
TypeScript Version: 2.0.0
TL;DR:
If a project is configured for strict null checks, checking a variable which is typed as non-null is nonsense and could indicate a type mismatch.
Code:
Here's a motivating example I experienced:
async function someFunc1(): Promise<string | null> {
// do some stuff and return the appropriately typed promise
}
async function someFunc2(): Promise<boolean> {
// Negating a non-null Promise is crazy talk, but here I go!
return !!someFunc1()
}
Of course what I really meant was:
async function someFunc2(): Promise<boolean> {
return !!(await someFunc1())
}
Expected behavior:
TypeScript warns that I'm effectively testing my non-null Promise for null, and points out that that's nonsense.
Actual behavior:
TypeScript happily accepted the code, which led to a bug.
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
In der Issue wird keine Datei, kein Test und kein Einstiegspunkt genannt. Beginne damit, das in den Beispielen beschriebene Verhalten von strict-null-checking zu überprüfen, und verfolge, wie TypeScript Nulltests für nicht nullfähige Ausdrücke behandelt; fertig ist die Aufgabe, wenn die inkorrekte Prüfung eine Diagnose erzeugt, ohne gültige nullable Prüfungen abzulehnen.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Feature
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100