microsoft / microsoft/TypeScript
`keyof` on homomorphic mapped type over an array thinks keys are function types
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the behavior in the linked TypeScript Playground using the Keys example with string[]. Start by investigating how the compiler handles keyof and homomorphic mapped types for arrays; done means Keys<string[]> resolves to the array's property-name union rather than a union of functions.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100