isFunction()
isFunction(
value):value is Function
Checks if a value is a function.
note
Includes arrow functions, regular functions, async functions, and class constructors.
Parametersβ
value: unknownβ
The value to check.
Returns: value is Functionβ
true if the value is a function, false otherwise.
Sinceβ
1.0.0
Exampleβ
isFunction(() => {}); // => true
isFunction(function() {}); // => true
isFunction(async () => {}); // => true
isFunction(class {}); // => true
isFunction({}); // => false
How it works?β
Type guard that checks if a value is a function.
Type Narrowingβ
Common Checksβ
| Value | Result |
|---|---|
() => {} | true |
function() {} | true |
async () => {} | true |
class Foo {} | true |
Array.isArray | true |
{ call: () => {} } | false |
Use Casesβ
Execute callbacks safely πβ
Verify if a property is a callable function before invocation. Essential for event handlers and optional callback execution.
if (isFunction(props.onComplete)) {
props.onComplete(result);
}
Resolve value-or-getter patternsβ
Handle props that can be either static values or functions returning values. Common pattern in React and configuration APIs.
type MaybeGetter<T> = T | (() => T);
function resolve<T>(value: MaybeGetter<T>): T {
return isFunction(value) ? value() : value;
}
const title = resolve(props.title); // Works with "Hello" or () => "Hello"
true
false