microsoft / microsoft/TypeScript

`keyof` on homomorphic mapped type over an array thinks keys are function types

Offen
#44,217 4 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bug Domain: Index Types
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

Bug Report

Creating a mapped type (where each value is just the key - { [K in keyof T]: K }) when applied over a generic type T, where T has been passed as an array type, somehow manages to resolve the keys of an array as functions. I've read the FAQ about homomorphic mapped types but I can't imagine the K in K in keyof T ending up as a function type - no matter what the T eventually becomes.

🔎 Search Terms

keyof, array, types, type constructors

🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about homomorphic mapped types

Tested on 3.9.7, 4.2.3.

⏯ Playground Link

Playground link with relevant code

💻 Code
type Keys<T> = {
    [K in keyof T]: K;
}[keyof T];

type GT = Keys<string[]>;
// ^ Hover here. Keys are functions ???

Type upon hovering

🙁 Actual behavior

Keys applied over an array, returns a union of functions.

🙂 Expected behavior

Keys applied over an array, should return the property names of the array.

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

Reproduziere das Verhalten im verlinkten TypeScript Playground anhand des Keys-Beispiels mit string[]. Untersuche zunächst, wie der Compiler keyof und homomorphe gemappte Typen für Arrays verarbeitet; fertig ist es, wenn Keys<string[]> sich in die Union der Eigenschaftsnamen des Arrays statt in eine Union von Funktionen auflöst.

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
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

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