microsoft / microsoft/TypeScript
A contextually-typed signature coming from correlated union can't be assigned back to source of its contextual type
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
🔎 Search Terms
correlated union signature union map mapped type index
🕗 Version & Regression Information
- This is the behavior in every version I tried
⏯ Playground Link
💻 Code
type Definitions = {
onFoo: [arg: number];
onBar: [arg: string];
};
type SomeCallbacks = {
[K in keyof Definitions]: (...args: Definitions[K]) => void;
};
const wrapCallback = <K extends keyof SomeCallbacks>(
source: SomeCallbacks,
target: SomeCallbacks,
key: K,
) => {
const callback = source[key];
target[key] = (...args) => {
if (Math.random() > 0.5) {
return callback(...args);
}
};
};
function wrapAll(callbacks: SomeCallbacks): SomeCallbacks {
const wrapped = {} as SomeCallbacks;
for (let key in callbacks) {
wrapCallback(callbacks, wrapped, key as keyof SomeCallbacks);
}
return wrapped;
}
🙁 Actual behavior
It fails to recognize that this function is assignable to target[key] despite the fact that args are contextually-typed by that target[key]
🙂 Expected behavior
I'd expect this to typecheck just fine
Additional information about the issue
No response
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia riproducendo l’errore nel TypeScript Playground collegato usando l’esempio fornito di unione correlata e tipo mappato. Traccia il percorso del controllo dei tipi per il callback tipizzato contestualmente e la sua assegnazione a target[key]; il lavoro è completato quando l’esempio supera il controllo dei tipi come previsto, senza indebolire le relazioni tra i tipi riportate.
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
- Abbastanza chiara
- Idoneità per principianti
- 25/100