microsoft / microsoft/TypeScript
Misleading error message "'{}' and 'number' have no overlap"
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
🔎 Search Terms
empty object type, unintentional comparison
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about object types and structural typing
⏯ Playground Link
💻 Code
if ({} === 0) {}
🙁 Actual behavior
TS 2367: "This comparison appears to be unintentional because the types '{}' and 'number' have no overlap."
🙂 Expected behavior
TS 2367: "This comparison appears to be unintentional because the types 'object' and 'number' have no overlap."
Additional information about the issue
the type {} and number do have overlap; number is a subset of {}.
declare const n: number;
// allowed!
const emptyObjectType: {} = n;
TS is correct to report an error, but it should give a technically correct reason for the error.
Analogous (very contrived) examples can be made for similar coincidentally-including-primitive types, like
// This comparison appears to be unintentional because the types '{ toFixed(): string; toPrecision(): string; }' and 'number' have no overlap.
if ({ toFixed(): string { return "" }; toPrecision(): string { return "" }} === 0) {}
// yet this is allowed
const numbery: { toFixed(): string; toPrecision(): string; } = 0;
Stems from discord discussion beginning here: https://discord.com/channels/508357248330760243/508357638677856287/1310419206260654081
Closely related to discussion regarding https://github.com/microsoft/TypeScript/issues/60582
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 verknüpfte TypeScript Playground-Reproduktion auszuführen und der Diagnose TS2367 zu folgen. Vergleiche die gemeldete Formulierung '{}' und 'number' mit der erwarteten Formulierung 'object' und 'number', füge anschließend Abdeckung für den gezeigten Vergleich hinzu oder aktualisiere sie und überprüfe, dass die Diagnose für die zugehörigen Beispiele weiterhin korrekt ist.
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
- Größtenteils klar
- Anfängerfreundlichkeit
- 45/100