Functions & Promises
Validate function arguments and returns, and unpack asynchronous Promise resolutions.
Function Wrappers: infer.function()
Wraps a function such that incoming arguments are validated at invocation, and return values are validated before returning:
Schema Definition
const SumFunction = infer.function(
infer.tuple([infer.number(), infer.number()]),
infer.number()
);
const add = SumFunction.parse((a: number, b: number) => a + b);
add(10, 20); // 30Promise Schemas: infer.promise()
Validates that the input is a Promise and parses its resolved value asynchronously:
Schema Definition
const AsyncString = infer.promise(infer.string());
const resolved = await AsyncString.parseAsync(Promise.resolve("hello"));