parseCallable
Parses a value into a callable function. This parser combines the validateCallable protocol validator with the normalizeCallable normalizer to ensure that the input is a function that can be invoked with () without throwing a TypeError.
The parser never throws, never mutates input, and never invokes the function. It returns a structured ParseResult<(...args: unknown[]) => unknown> describing success or failure.
Signature
function parseCallable<
T extends (...args: unknown[]) => unknown = (...args: unknown[]) => unknown
>(
value: unknown
): ParseResult<T>
Behavior
- Accepts:
- normal functions
- async functions
- generator functions
- bound functions
- Rejects:
- ES2015 class constructors.
- Primitives.
nullandundefined.- Objects without callable behavior.
- Functions with hostile or malformed
toStringimplementations. - Never invokes the function.
- Never throws and never mutates input.
Returns
ok: true: And the original function when parsing succeeds.ok: false, value: null: And a list of issues when parsing fails.
Examples
parseCallable(() => 1)
// { ok: true, value: [Function], issues: [] }
class Foo {}
parseCallable(Foo)
// { ok: false, value: null, issues: ["Value is not a callable function"] }
Notes
- This parser is ideal for safely handling user-provided callbacks, hooks, handlers, and plugin interfaces.
- Pure, side-effect-free, and safe for all runtime environments.