Skip to content

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

Closed as not planned

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

  1. RyanCavanaugh commented on Feb 27, 2023

    @RyanCavanaugh
    Member

    When the type of action is known to be a mapped type, we can take a smarter analysis of action[move.type](move) to realize that the call is OK by analyzing the mapping rather than the possible concrete instantiations of K.

    When it isn't, the only way to "see" that this call is OK is by brute enumeration of all possible inhabitants of K, which in general is not feasible from a performance perspective.

    So in this case, we have to trade a little bit of completeness for a big gain in performance.

  2. jcalz commented on Feb 27, 2023

    @jcalz
    Contributor

    Cross-linking with #47109, which describes the working technique specifically as a way of dealing with the compiler's inability to efficiently scry into the multiverse of possible narrowings.

  3. RyanCavanaugh commented on Feb 27, 2023

    @RyanCavanaugh
    Member

    Thanks jcalz, I can never find that one when I need to πŸ˜…

  4. Andarist commented on Feb 27, 2023

    @Andarist
    ContributorAuthor

    Yeah, so I know about this limitation. This issue is specifically about the satisfies. Here, the type is contextually typed through the satisfies operator and the mapped type is involved in creating this type. Therefore TS wouldn't have to figure out this relationship from scratch - it could be preserved on some link or something and utilized by the checker.

  5. RyanCavanaugh commented on Feb 28, 2023

    @RyanCavanaugh
    Member

    The eternal war between "TypeScript should always behave simply and consistently, even if it's a little bit worse" and "You can gerrymander in a little bit of logic to make things work when they otherwise wouldn't have" rages on.

  6. fatcerberus commented on Feb 28, 2023

    @fatcerberus

    The eternal war ... rages on

    ...so shut up and put on your poncho. You're in the splash zone. πŸ˜‰

  7. github-actions commented on Jun 8, 2023

    @github-actions

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

  8. 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

    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