microsoft / microsoft/TypeScript

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

Offen
#59,102 3 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Domain: check: Type Inference Help Wanted Possible Improvement
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

🔎 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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit dem verknüpften TypeScript Playground und vergleiche die in der bereitgestellten Reproduktion inferierten Typen Ids und IdsCopy. Verfolge, wie das Spreaden eines as const-Objekts keyof typeof und die satisfies Record<number, string>-Constraint beeinflusst. Als erledigt gilt die Aufgabe, wenn die Schlüssel des kopierten Objekts die im erwarteten Verhalten gezeigte numerische Literal-Union beibehalten.

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
Klar beschrieben
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.