isFunction()
isFunction(
value):value is Function
Checks if a value is a function.
remarque
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