microsoft / microsoft/TypeScript
Incorrect union type inference for conjunction with strictNullChecks disabled
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: 3.4.5
Search Terms: union type inference conjunction strictNullChecks
Code
function fn(x: number) {
return x && 'a'
}
Expected behavior:
Return type of fn should be 0 | 'a'
Actual behavior:
Return type of fn is '' | 'a' if strictNullChecks is disabled
This is causing me an issue with this specific bit of code, where I'm checking window so my code runs both on the browser and on the client:
function fn(x: number): OrientationType | undefined {
return window && (window.outerWidth > window.outerHeight ? 'landscape-primary' : 'portrait-primary')
}
I can replace the conjunction with a conditional expression, and I know that if strictNullChecks is disabled I should have a fallback for the return value but I'm posting here for the sake of trying to understand if this is working as intended. It also feels weird that my example compiles with strictNullChecks enabled but doesn't compile when it's disabled.
Related Issues:
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 Beispiele im verknüpften TypeScript Playground mit deaktiviertem und aktiviertem strictNullChecks zu reproduzieren. Verfolge, wie der Typprüfer den Konjunktionsausdruck in fn(x: number) ableitet. Als erledigt gilt die Aufgabe, wenn die deaktivierte Einstellung den erwarteten Rückgabetyp 0 | 'a' erzeugt, ohne die aktivierte Einstellung oder das window-Beispiel zu beeinträchtigen.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Klar beschrieben
- Anfängerfreundlichkeit
- 35/100