I have a specific function in my code:
function test(cb: Function | number) {
let item = { height: 0}
if(typeof cb === 'number') {
item.height = cb;
}
if(typeof cb === 'object') {
item.height = cb();
}
}
This function accepts either a number or a callback function as an argument. If it's a number, the height is set directly. If it's a function, the function is called and its return value (a number) is used for the height.
function getNumber() {
return 2;
}
test(getNumber);
However, I keep encountering an error when I try to do item.height = cb();
No constituent of type 'number | Function' is callable.
How can I resolve this issue without resorting to using any
? Using any
would make the error disappear but it's not a recommended solution.