Skip to main content
is.function creates a guard that validates function signatures at runtime. It checks that a value is a callable, and optionally validates both the arguments passed to it and the value it returns without any changes to the function’s own source code. The primary use case is wrapping an existing function with a validated shell using .impl (or one of its async/Result variants). The shell enforces the declared contract every time the function is called.

Creating a function guard

  • input (required) — an ordered tuple of guards, one per argument position.
  • output (optional) — a guard for the return value. Omit it to allow any return type.
The guard itself is a standard guard: calling it as a predicate (guard(v)) returns true if v is any function, and false otherwise. The input and output guards are not invoked during the predicate check.
The inferred TypeScript type of the guard reflects the declared signature:

.impl — synchronous validated wrapper

.impl wraps a function so that every call validates arguments first and the return value second. If validation fails, an AggregateGuardError is thrown.
Error paths use args[N] for argument positions and return for the output:
The wrapper is transparent: the return type, arity, and parameter names are preserved by TypeScript.

.implAsync — async validated wrapper

Use .implAsync when the function returns a Promise. Inputs are validated synchronously (before the function runs). The resolved value is validated after the promise settles.

.implResult and .implResultAsync — non-throwing variants

If you prefer not to use exceptions for validation failures, use the Result-returning variants. They wrap the same logic but return Result<T, AggregateGuardError> instead of throwing.
The async equivalent returns ResultAsync<T, AggregateGuardError>:

Omitting output

When output is omitted, the return value is not validated and TypeScript infers its type from the implementation:

Using complex guards for inputs and outputs

Any guard expression works, including objects, arrays, unions, and chained helpers:

Using is.function as a predicate

Because is.function produces a standard guard, it composes with all other guards. You can use it to validate that a field on an object is a function of a specific shape:

Error shape: AggregateGuardError

Thrown by .impl and .implAsync when validation fails. It is an Error subclass with two extra properties: The .message property summarizes the first three errors inline. For structured error reporting you can iterate .errors directly:

Summary

All four variants:
  1. Validate each argument against its position’s guard before calling the function
  2. Call the original function only if inputs pass
  3. Validate the return value against output (if provided) before returning to the caller