I cannot get the compiler to know a type of a callback argument.
function test3(fn: (foo: string) => any): any;
function test3(fn: (foo: string, payload: number) => any): any;
function test3(fn: (foo: string, payload: number) => any) {
}
test3((foo) => 1); // Ok, typescript knows "foo" is a string
test3((foo, payload) => 1); // KO, typescript does not infer "foo" nor "payload" type
I don't understand why on the second call, i'd have to manually write the type of foo, but not in the first call.
Can the infer work with these overloads ? If yes, how ? If not, why does it not work ?
// change the order for more specific type first
function test3(fn: (foo: string, payload: number) => any): any;
function test3(fn: (foo: string) => any): any;
function test3(fn: (foo: string, payload: number) => any) {
}
test3((foo) => 1);
test3((foo, payload) => 1);
Playground
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With