I have the following function and its callback type:
type Callbacks = {
onSuccess: (a: string) => void;
};
function t(event: string, ...args: [...any, Callbacks]) {
}
It works as expected but one thing, onSuccess
function has a string param but TS can't recognize it and says that it has any type but I explicitly set it to string.
t("eventName", "bobo", 123, {onSuccess: (asd) => {
// "asd" is a string but TS says that it's an any
// Parameter 'asd' implicitly has an 'any' type
}})
What should I change in order to let TS recognize the callback's params type because manually specifying them every time is tedious?
P.S. it's a simplified example of my problem
CodePudding user response:
This is currently a missing feature in TypeScript. A function whose parameter list has leading or middle rest elements does not seem to benefit from contextual typing of its input parameters when called. There is a request at microsoft/TypeScript#45972 to change this. It's (as of 2022-07-06) marked as "awaiting more feedback", so if you want to see this changed it wouldn't hurt to give it a