I have a function called myCallback
const myCallback = (param: number) => {
// doSomething
};
Now, I want to create another function, useMyCallback
, that may or may not receive a parameter and will return myCallback
either bound or unmodified:
const useMyCallback = (optionalParam?: number) => {
return optionalParam === undefined
? myCallback
: () => myCallback(optionalParam);
};
The issue is that regardless of whether I pass in a parameter or not, the return value of myCallback
is always typed as (param: number) => void
const myCallback = (param: number) => {
// doSomething
};
const useMyCallback = (optionalParam?: number) => {
return optionalParam === undefined
? myCallback
: () => myCallback(optionalParam);
};
const unboundCallback = useMyCallback(); // returns (param: number) => void as expected
const boundCallback = useMyCallback(1); // returns (param: number) => void, when expecting () => void
Is there a way to achieve what I want without having to cast each useMyCallback
call?