Skip to content

normalizeCallable

Guarantees a callable value or returns null.

This helper determines whether a value is a JavaScript function and returns it unchanged if so. It uses an explicit callable signature instead of the unsafe Function type, ensuring strong type safety without affecting runtime behavior.

Use this helper when you need to safely accept callbacks, handlers, or user-provided functions without invoking them.

Signature

function normalizeCallable<
  T extends (...args: unknown[]) => unknown = (...args: unknown[]) => unknown
>(value: unknown): T | null

Parameters

Name Data type Description
value unknown The value to normalize.

Returns

One of:

  • The original value, typed as a callable function.
  • null: If the value is not callable.

Behavior

  • Accepts:
  • Standard functions.
  • Arrow functions.
  • Async functions.
  • Generator functions.
  • Bound functions.
  • Class constructors.
  • Rejects:
  • null and undefined.
  • Objects
  • Arrays
  • Primitives
  • Never throws and never mutates input.
  • Never invokes the function.

Examples

normalizeCallable(() => {})
// [Function]

normalizeCallable(async () => {})
// [AsyncFunction]

normalizeCallable(class Example {})
// [class Example]

normalizeCallable(123)
// null

With explicit typing:

const fn = normalizeCallable<(x: number) => string>(value);

if (fn) {
  fn(42); // string
}

Notes

This helper does not:

  • Invoke functions
  • Inspect parameters or return values
  • Perform structural heuristics