isFunction
Checks if a value is a function.
Implementation
View Source Code
ts
/**
* Determines if the passed value is a function.
*
* @example
* ```ts
* isFunction(function() {}) // true
* isFunction(() => {}) // true
* isFunction('hello world') // false
* ```
*
* @param arg - The argument to be checked.
*
* @returns `true` if the value is a function, else `false`.
*/
// biome-ignore lint/suspicious/noExplicitAny: -
export function isFunction(arg: unknown): arg is (...args: any[]) => any {
return typeof arg === 'function';
}
export const IS_FUNCTION_ERROR_MSG = 'Expected a function';Features
- Type Guard: Narrows
unknowntoFunction - Isomorphic: Works in both Browser and Node.js
API
ts
function isFunction(value: unknown): value is Function;Parameters
value: The value to check
Returns
trueif the value is a function,falseotherwise
Examples
Basic Usage
ts
import { isFunction } from '@vielzeug/toolkit';
isFunction(() => {}); // true
isFunction(function () {}); // true
isFunction(async () => {}); // true
isFunction(123); // false
isFunction(null); // falseType Guard Usage
ts
import { isFunction } from '@vielzeug/toolkit';
function execute(value: unknown) {
if (isFunction(value)) {
// TypeScript knows value is Function here
return value();
}
return null;
}Implementation Notes
- Returns
truefor all function types (regular, arrow, async, generators) - Returns
truefor class constructors - Useful for callback validation and type narrowing
See Also
- isObject: Check if value is an object
- isPromise: Check if value is a Promise
- typeOf: Get the type of any value