Skip to content
VersionSize

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 unknown to Function
  • Isomorphic: Works in both Browser and Node.js

API

ts
function isFunction(value: unknown): value is Function;

Parameters

  • value: The value to check

Returns

  • true if the value is a function, false otherwise

Examples

Basic Usage

ts
import { isFunction } from '@vielzeug/toolkit';

isFunction(() => {}); // true
isFunction(function () {}); // true
isFunction(async () => {}); // true
isFunction(123); // false
isFunction(null); // false

Type 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 true for all function types (regular, arrow, async, generators)
  • Returns true for class constructors
  • Useful for callback validation and type narrowing

See Also