Microsoft/TypeScript

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

Open

#54.824 geöffnet am 29. Juni 2023

Auf GitHub ansehen
 (0 Kommentare) (0 Reaktionen) (0 zugewiesene Personen)TypeScript (6.726 Forks)batch import
Domain: Indexed Access TypesHelp WantedPossible Improvement

Repository-Metriken

Stars
 (48.455 Stars)
PR-Merge-Metriken
 (Durchschn. Merge 6T 17h) (9 gemergte PRs in 30 T)

Beschreibung

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)

Contributor Guide