Skip to content

isFunction function ​

Checks whether a value is callable: a function or a class.

ts
import { isFunction } from 'utils';

Signature ​

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

Parameters ​

NameDescription
valueThe value to check.

Returns ​

true for any function.

Example ​

ts
isFunction(() => 0); // true
isFunction({}); // false

Tests ​

Line coverage: 100 %.

  • ✅ accepts functions and classes

Source ​

src/guard/is-function.ts (on GitHub)

Source code
ts
/**
 * Checks whether a value is callable: a function or a class.
 *
 * @param value - The value to check.
 * @returns `true` for any function.
 * @example
 * isFunction(() => 0); // true
 * isFunction({}); // false
 */
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type -- The type every function is assignable to.
export function isFunction(value: unknown): value is Function {
  return typeof value === 'function';
}
Test file
ts
import { isFunction } from './is-function';

class Point {
  public readonly x = 0;
}

// Plain function type: calls are not seen as conditions, so any value can be passed.

describe(isFunction, () => {
  it('accepts functions and classes', () => {
    expect(isFunction(() => 0)).toBe(true);
    expect(isFunction(Point)).toBe(true);
    expect(isFunction({})).toBe(false);
  });
});

Keywords: is, function, guards

Generated from the JSDoc of the sources.