Skip to main content

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โ€‹

ValueResult
() => {}checkmark true
function() {}checkmark true
async () => {}checkmark true
class Foo {}checkmark true
Array.isArraycheckmark true
{ call: () => {} }cross 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"