Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Andarist opened this issue Feb 27, 2023 · 7 comments
Labels
Not a Defect This behavior is one of several equally-correct options

Comments

@Andarist
Copy link
Contributor

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.

@RyanCavanaugh
Copy link
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.

@RyanCavanaugh RyanCavanaugh added the Not a Defect This behavior is one of several equally-correct options label Feb 27, 2023
@jcalz
Copy link
Contributor

jcalz commented Feb 27, 2023

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.

@RyanCavanaugh
Copy link
Member

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

@Andarist
Copy link
Contributor Author

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.

@RyanCavanaugh
Copy link
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.

@fatcerberus
Copy link

The eternal war ... rages on

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

@github-actions
Copy link

github-actions bot commented Jun 8, 2023

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

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jun 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Not a Defect This behavior is one of several equally-correct options
Projects
None yet
Development

No branches or pull requests

4 participants