microsoft / microsoft/TypeScript

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

Aperta
#54,824 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Domain: Indexed Access Types Help Wanted Possible Improvement
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

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)

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con le riproduzioni collegate in TypeScript Playground e confronta la firma basata su un'intersezione con la variante basata su Extract. Determina perché le due chiamate che accettano "" e 100 non producono alcun errore in una versione, quindi aggiungi un test di regressione e assicurati che le chiamate non sicure vengano rifiutate.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.