microsoft / microsoft/TypeScript
Nominal/branded key for `Record` disables checking/inferring of value type
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
Bug Report
🔎 Search Terms
nominal, Record, branded
🕗 Version & Regression Information
3.3.3 through 4.3.2
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about nominal types and record types
⏯ Playground Link
Playground link with relevant code
💻 Code
type Id = string & { __id: 'id' }
type User = {
id: Id;
name: string;
}
type Users = Record<Id, User>;
type Users2 = Map<Id, User>;
type Users3 = Record<string, User>;
// This will not produce an error
// @ts-expect-error No name should be an error!
const users: Users = { id0: { id: 'id', } }
// type: [string, unknown][]
const entries = Object.entries(users);
const users2: Users2 = new Map();
// @ts-expect-error No name produces an error
users2.set('id' as Id, { id: 'id' as Id, })
// @ts-expect-error No name produces an error
const users3: Users3 = { id0: { id: 'id' as Id, } }
// type: [string, User][]
const entries3 = Object.entries(users3);
🙁 Actual behavior
Using a nominal/branded type as the key to a record type causes type checking of the value to be skipped. Additionally, inference will also fail and fallback to unknown, such as using Object.entries. Using a Map works as expected, but not a Record. I realize that the runtime behavior between a Map (real) and Record (type) is completely different, I'm just providing it as an example.
🙂 Expected behavior
I'd expect types to be preserved and checked, even when using a nominal/branded type as a key to a record.
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
Inizia eseguendo la riproduzione collegata in TypeScript Playground con gli esempi Record<Id, User> e Object.entries, quindi confrontali con i casi funzionanti Map e Record con chiavi stringa. Traccia la gestione da parte del compilatore delle chiavi branded e dei valori dei record; il lavoro è completo quando il controllo dei valori segnala la mancanza di name e Object.entries(users) conserva User invece di inferire unknown.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 38/100