Skip to content

Mapped types key remapping as K is converting array/tuple into objectΒ #52264

Closed

Description

Bug Report

πŸ”Ž Search Terms

Generics, Mapped types, key remapping, Array, Tuple

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type Copy1<T> = {[K in keyof T]: T[K]}

type Copy2<T> = {[K in keyof T as K]: T[K]}

type Test1 = Copy1<[1, 2, 3]> // good - [1, 2, 3]

type Test2 = Copy2<[1, 2, 3]> // bad - { [x: number]: 3 | 1 | 2, toString: () => string, ... }

πŸ™ Actual behavior

Trivial as K key remapping is causing tuple/array to be converted into an object (yes, same thing for arrays)

πŸ™‚ Expected behavior

trivial key remapping should not change the behavior of the mapped type. Tuple/array should not become object since TS 3.1. Copy1 and Copy2 should be equivalent

Notes:
Of course, it's a simplified example. In my actual use case, I had a conditional type like as T extends unknown[] ? K : .... The workaround was to move the conditional type out of the mapped type and write the mapped type twice

Activity

  1. changed the title [-]Mapped types key remappin `as K` is convertin array into object[/-] [+]Mapped types key remapping `as K` is converting array/tuple into object[/+] on Jan 17, 2023
  2. jcalz commented on Jan 17, 2023

    @jcalz
    Contributor

    Duplicate of #40586

  3. typescript-bot commented on Feb 5, 2023

    @typescript-bot
    Contributor

    This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

  4. locked as resolved and limited conversation to collaborators on Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions