Skip to content

keyof collapses the expression when used in a mapped typeΒ #55129

Closed
@Mad-Kat

Description

@Mad-Kat

Bug Report

πŸ”Ž Search Terms

  • keyof union generic
  • keyof union
  • keyof

πŸ•— Version & Regression Information

  • Since 4.1.5 (Playground test where mapping works)

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type Fruit =
  | {
      name: "apple";
      color: "red";
    }
  | {
      name: "banana";
      color: "yellow";
    }
  | {
      name: "orange";
      color: "orange";
    };

type Result1<T extends {name: string | number; color: string | number }> = {
  [Key in T as `${Key['name']}:${Key['color']}`]: unknown
}; 
type Result2<T extends {name: string | number; color: string | number }> = keyof {
  [Key in T as `${Key['name']}:${Key['color']}`]: unknown
}

// should be "apple:red" | "banana:yellow" | "orange:orange"
type Test1 = keyof Result1<Fruit> // this works

// should be "apple:red" | "banana:yellow" | "orange:orange"
type Test2 = Result2<Fruit> // this doesn't work and I'm not sure why :D

πŸ™ Actual behavior

  • Test2 is "apple:red" | "apple:yellow" | "apple:orange" | "banana:red" | "banana:yellow" | "banana:orange" | "strawberry:red" | "strawberry:yellow" | "strawberry:orange"
  • Somehow Result2 is collapsed:
    image

πŸ™‚ Expected behavior

  • Test2 should be "apple:red" | "banana:yellow" | "orange:orange"

Metadata

Metadata

Assignees

No one assigned

    Labels

    Possible ImprovementThe current behavior isn't wrong, but it's possible to see that it might be better in some cases

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions