type MaybeCallable<T> = T | (() => T);
function invokeMaybeCallable<T>(value?: MaybeCallable<T>): T | undefined {
if (value === undefined) {
return undefined;
} else if (typeof value === "function") {
// Error:
// This expression is not callable.
// Not all constituents of type '(() => T) | (T & Function)' are callable.
// Type 'T & Function' has no call signatures
return value();
} else {
return value;
}
}
#`typeof value == "function"` is not callable
1 messages · Page 1 of 1 (latest)
typeof value == "function" is not callable
I guess I could add this but I figured there's an easier way:
const isCallback = <T,>(maybeFunction: MaybeCallable<T>,): maybeFunction is () => T => typeof maybeFunction === 'function';
T might be a function, in which case you wouldn't know how to call it correctly
Don't I lock that down with it being MaybeCallable<T>?
no, that ambiguates it
i don't know your specific context, but in general if you just ues a definite function and always call it that might be easier to manage
declare const x: MaybeCallable<(a: string) => number>
```here, `x` is either that `(a: string) => number`, or a function that returns `(a: string) => number`
you can't differentiate them
Yeah I just understood what you meant
That makes sense..
function invokeMaybeCallable<T extends string | number | boolean>(value?: MaybeCallable<T>): T | undefined {
if (value === undefined) {
return undefined;
} else if (typeof value === "function") {
return value();
} else {
return value;
}
}
That works for me, thanks!