microsoft / microsoft/TypeScript

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

Open
#44,217 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: Index Types
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 ???

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.