microsoft / microsoft/TypeScript
ts2367 Comparing non-comparable types not detected with optional property
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.6.0-dev.20190730 (also with 3.5.1)
Search Terms: compare types, comparable, always false comparison, optional property, properties
Code
enum X {
A= 'A'
}
function x(res: {action: string}):boolean {
return res === X.A // error
}
function y(res: {action: string | undefined}):boolean {
return res === X.A // error
}
function z(res: {action?: string}):boolean {
return res === X.A // no error, but always false
}
function correct(res: {action: string}):boolean {
return res.action === X.A // what the code should have been
}
Expected behavior:
The comparison in function z should have resulted in error TS2367: This condition will always return 'false' since the types '{ action?: string; }' and 'X.A' have no overlap.
Actual behavior:
Functions x, y give the correct error. Function z has no compiler error, but the condition will always evaluate to false since it is comparing an object and a string.
Related Issues: n/a
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 mit der minimalen Reproduktion in den Funktionen x, y und z oder öffne das verlinkte TypeScript Playground. Vergleiche die Diagnosen für erforderliche, explizit auf undefined gesetzte und optionale Eigenschaften. Fertig ist die Aufgabe, wenn der Vergleich in z wie x und y TS2367 meldet und das bestehende Verhalten für die anderen Beispiele erhalten bleibt.
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