validateCallable
Checks whether a value is callable — meaning it can be invoked with () without throwing a TypeError due to non-callability. This helper uses the isCallable type guard internally.
A callable value must be a function that is not an ES2015 class constructor. Class constructors have typeof "function" but cannot be invoked without new, so they are intentionally rejected.
This validator never throws and never mutates input. Use it when you need to ensure a value is a safe, callable function before invoking it.
Signature
function validateCallable(
value: unknown
): ((...args: unknown[]) => unknown) | undefined
Parameters
| Name | Data type | Description |
|---|---|---|
| value | unknown |
The value to validate. |
| field | string |
The name of the field being validated, used in error reporting. |
Returns
One of:
- The original function if the value is callable.
- undefined if the value is not callable.
Behavior
- Uses isCallable internally.
- Accepts:
- Normal functions.
- Arrow functions.
- Functions with parameters.
- Functions with non-callable toString properties.
- Rejects:
- ES2015 class constructors.
- Functions whose
toStringindicates a class. - Functions with hostile or throwing
toStringgetters. - Non-function values.
- Never throws and never mutates input.
- Never coerces values.
Examples
validateCallable(() => 123)
// () => 123
validateCallable(function (a, b) { return a + b })
// function (a, b) { ... }
validateCallable(class X {})
// undefined
validateCallable(123)
// undefined
Notes
- This validator does not attempt to invoke the function; it only checks callability.
- It is resilient to hostile or malformed
toStringimplementations. - Suitable for validating callbacks, handlers, and user-provided functions.
- The validator is pure, side-effect-free, and never throws