microsoft / microsoft/TypeScript

Inconsistent key type inference when spreading objects with const assertion into plain objects

Aperta
#59,102 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Domain: check: Type Inference Help Wanted Possible Improvement
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

🔎 Search Terms

const assertion, type inference, as const, object keys, type conversion, keyof typeof, satisfies, spread, readonly object

🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about keyof, as const, infererence etc.
⏯ Playground Link

Playground

💻 Code
const namesById = {
  1: "John Doe",
  2: "Jane Smith",
  3: "Alex Johnson",
  4: "Maria Garcia",
} as const satisfies Record<number, string>;

type Ids = keyof typeof namesById;
//   ^? type Ids = 1 | 2 | 3 | 4

const namesByIdCopy = {
  ...namesById,
} satisfies Record<number, string>;

type IdsCopy = keyof typeof namesByIdCopy;
//   ^? type IdsCopy = "1" | "2" | "3" | "4"

🙁 Actual behavior

IdsCopy has converted all of the keys to string literal union instead of a number literal union when using keyof typeof, even despite the fact that we've used satisfies Record<number, string> on the namesByIdCopy object.

🙂 Expected behavior
type IdsCopy = keyof typeof namesByIdCopy;
//   ^? type IdsCopy = "1" | "2" | "3" | "4"

should instead be typed as:

type IdsCopy = keyof typeof namesByIdCopy;
//   ^? type IdsCopy = 1 | 2 | 3 | 4
Additional information about the issue

The type inference seems to work as expected (i.e. numeric literal union) if as const is used on both the original object and the copy object, or if as const isn't used on the original object. It's only when spreading the object with anas const and inferring the keys using keyof typeof that they get inferred incorrectly.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con il TypeScript Playground collegato e confronta i tipi inferiti Ids e IdsCopy nella riproduzione fornita. Analizza come lo spread di un oggetto as const influisce su keyof typeof e sul vincolo satisfies Record<number, string>. Il lavoro è completato quando le chiavi dell’oggetto copiato mantengono l’unione di letterali numerici mostrata nel comportamento previsto.

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
Specificata chiaramente
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.