microsoft / microsoft/TypeScript
`keyof` on homomorphic mapped type over an array thinks keys are function types
Personne n'a encore pris cette issue.
- Langage dominant
- Go
- Étoiles
- 111k
- Forks
- 14.4k
- Merge moyen
- 1 j 19 h
- PR mergées (30 j)
- 117
Description
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 ???

🙁 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.
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Reproduisez le comportement dans le TypeScript Playground lié en utilisant l’exemple Keys avec string[]. Commencez par étudier comment le compilateur traite keyof et les types mappés homomorphes pour les tableaux ; c’est terminé lorsque Keys<string[]> se résout en l’union des noms de propriétés du tableau plutôt qu’en une union de fonctions.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- typescript
- Domaine
- compilers
- Type d'issue
- Bug
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 35/100