Skip to content

Fresh object literal typed through satisfies and mapped type lose the origin informationΒ #52995

Description

Bug Report

πŸ”Ž Search Terms

mapped type origin generic indexed access satisfies

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type MyMap = {
  "move-forward": { distance: number };
  stop: { cause: string };
};

type Values<T> = T[keyof T];

type Union<P extends keyof MyMap = keyof MyMap> = Values<
  {
    [K in P]: { type: K } & MyMap[K];
  }
>;

// const action: { [K in keyof MyMap]: (move: MyMap[K]) => void } = {
//   "move-forward": (move) =>
//     console.log("Moving forward by distance:", move.distance),
//   stop: (move) => console.log("Stopping. Cause:", move.cause),
// };
const action = {
  "move-forward": (move) =>
    console.log("Moving forward by distance:", move.distance),
  stop: (move) => console.log("Stopping. Cause:", move.cause),
} satisfies { [K in keyof MyMap]: (move: MyMap[K]) => void };

function execute<K extends keyof MyMap>(move: Union<K>) {
  action[move.type](move); // errors but it should be OK just like the variant with the declared type
}

πŸ™ Actual behavior

It errors on the action[move.type](move) call within the generic function.

πŸ™‚ Expected behavior

It should be OK just like the variant with the declared type.

Activity

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

    Not a DefectThis behavior is one of several equally-correct options

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions