Skip to content

Unsound calls allowed on a function coming from a deferred indexed access when intersection is involved #54824

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

Open
Andarist opened this issue Jun 29, 2023 · 0 comments
Labels
Help Wanted You can do this Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases
Milestone

Comments

@Andarist
Copy link
Contributor

Bug Report

πŸ”Ž Search Terms

deferred index access unsound call union intersection

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type A = {
  type: "A";
  value: string;
};

type B = {
  type: "B";
  value: number;
};

type Message = A | B;

export function handle<M extends Message>(callbacks: {
  [K in M["type"]]: (msg: (M & { type: K })["value"]) => unknown;
}) {
  window.addEventListener("message", (event) => {
    const msg = event.data as M;
    // should be an error because it's too permissive
    callbacks[msg.type as keyof typeof callbacks](msg.value);

    // one or the other must be wrong here
    callbacks[msg.type as keyof typeof callbacks]("");
    callbacks[msg.type as keyof typeof callbacks](100);
  });
}

πŸ™ Actual behavior

There is no error even though it should be based on Ander's comment here. Note that the comment is about a different variant of this signature, one using Extract. Both the Extract and intersection variants are essentially the same though - one errors today and one doesn't.

πŸ™‚ Expected behavior

I would expect to get an error here, just like in the Extract-based variant (TS playground)

@RyanCavanaugh RyanCavanaugh added the Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases label Jun 29, 2023
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Jun 29, 2023
@RyanCavanaugh RyanCavanaugh added the Help Wanted You can do this label Jun 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Help Wanted You can do this Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases
Projects
None yet
Development

No branches or pull requests

2 participants