CyrefJS
CCyrefJS
فا
Back to documentation
Type/Since 1.38.0

isFunction

Checks whether a value is a function and provides a TypeScript type guard for function narrowing.

isFunction(value):value is (...args: never[]) => unknown

Implementation

The function uses JavaScript's typeof operator and checks whether the result is exactly "function".

Type guard

The return type `value is (...args: never[]) => unknown` makes the function a TypeScript type guard. A successful check narrows the value to a callable function type.

typeof behavior

The check follows JavaScript's typeof semantics. This means callable values such as regular functions, arrow functions, async functions, and classes are reported as functions.

Parameters

valueunknownrequired

The value to check.

Returns

value is (...args: never[]) => unknown

Returns true when the value is a function; otherwise returns false. When true, TypeScript narrows the value to a callable function type.

Examples

01

Basic usage

Check whether a value is a function.

TypeScript
isFunction(() => {});
// true

isFunction("hello");
// false
02

Type narrowing

The type guard narrows an unknown value to a callable function type.

TypeScript
const value: unknown = () => "hello";

if (isFunction(value)) {
  value();
  // value is narrowed to (...args: never[]) => unknown
}
03

Named functions

Regular named functions are recognized as functions.

TypeScript
function greet() {
  return "Hello";
}

isFunction(greet);
// true
04

Async functions

Async functions are also recognized because typeof returns "function".

TypeScript
async function loadData() {
  return "data";
}

isFunction(loadData);
// true
05

Classes

JavaScript classes are also reported as functions by typeof.

TypeScript
class User {}

isFunction(User);
// true
06

Non-functions

Objects, strings, numbers, null, and undefined return false.

TypeScript
isFunction({});
// false

isFunction(42);
// false

isFunction(null);
// false

isFunction(undefined);
// false
Built with@cyref/js·Official documentation