Microsoft/TypeScript
Ver no GitHubA contextually-typed signature coming from correlated union can't be assigned back to source of its contextual type
Open
#61.676 aberto em 8 de mai. de 2025
Domain: check: Contextual TypesHelp WantedPossible Improvement
Métricas do repositório
- Stars
- (48.455 stars)
- Métricas de merge de PR
- (Mesclagem média 6d 17h) (9 fundiu PRs em 30d)
Description
🔎 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