microsoft / microsoft/TypeScript
Rethinking relationships between `{}` type, `object`, and primitives
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.4k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
🔎 Search Terms
empty object type, {}, type safety violation, unsound
🕗 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
function takesObject(x: object) {
// This rightly is a TS error.
if (x === 0) {
console.error('This branch should be unreachable');
}
}
const o: {} = 0;
// But this isn't, and should be.
takesObject(o);
🙁 Actual behavior
No error on takesObject(o). {} can be assigned to type object, despite {} meaning "all nonnullish values", whereas object means only JS object types.
🙂 Expected behavior
Error on takesObject(o). {} is a wider type than object.
Additional information about the issue
This stems from a typescript-eslint investigation into making no-unnecessary-condition be more correct around possibly-falsy "object types", such as {}, or even { toFixed(): string} (to which number may be assigned). See https://github.com/typescript-eslint/typescript-eslint/pull/10378. This also relates to previous (controversial) conversations about whether to flag the {} type with the linter, see, e.g. https://github.com/typescript-eslint/typescript-eslint/issues/8700.
I'm wondering if this was simply an oversight in https://github.com/microsoft/TypeScript/pull/49119, which aimed to fix soundness holes with {}?
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
Reproduziere die Zuweisbarkeitsfälle im verlinkten TypeScript Playground und lies anschließend den referenzierten TypeScript PR #49119 sowie die Untersuchung von typescript-eslint in PR #10378, um den Grenzbereich zwischen {} und object zu verstehen. Das Issue ist abgeschlossen, wenn der gemeldete Fall takesObject(o) die erwartete Diagnose erzeugt und verwandte primitive Fälle auf Regressionen geprüft wurden.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 30/100