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