microsoft / microsoft/TypeScript
Rethinking relationships between `{}` type, `object`, and primitives
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
🔎 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 {}?
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Riproduci i casi di assegnabilità nel TypeScript Playground collegato, poi leggi il PR #49119 di TypeScript a cui si fa riferimento e l’indagine di typescript-eslint nel PR #10378 per avere il contesto sul confine tra {} e object. L’issue è completa quando il caso segnalato takesObject(o) produce la diagnostica attesa e i casi primitivi correlati sono stati verificati per individuare eventuali regressioni.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 30/100