assertFunction()
export namespace functional {
export function assertFunction<T extends Function>(func: T): T;
export function assertParameters<T extends Function>(func: T): T;
export function assertReturn<T extends Function>(func: T): T;
export function assertEqualsFunction<T extends Function>(func: T): T;
export function assertEqualsParameters<T extends Function>(func: T): T;
export function assertEqualsReturn<T extends Function>(func: T): T;
}
Asserts a function.
typia.functional.assertFunction<T>()
asserts a function, by wrapping the parameter function and checking its parametrs and return value through typia.assert<T>()
function. If some parameter or return value does not match the expected type, it throws a TypeGuardError
error.
For reference, TypeGuardError.path
would be a little bit different with individual typia.assert<T>()
function. If TypeGuardError
occurs from some parameter, the path wouold start from $input.parameters[<index>]
. Otherwise the path would start from $input.return
.
$input.parameters[0].~
$input.return.~
By the way, if you don’t want to assert both paramters and return value, but one of them, you can use typia.functional.assertParameters<T>()
or typia.functional.assertReturn<T>()
instead. Otherwise, if you want to prohibit superfluous properties, typia.functional.assertEqualsFunction<T>()
would be helpful.
Also, if what you want is not just finding the first type error through assertion, but also finding every type errors, utilize typia.functional.validateFunction<T>()
function instead. Otherwise, you don’t need the reason why, but just want to know whether the function is valid or not, use typia.functional.isFunction<T>()
function.
import typia from "typia";
const func = typia.functional.assertFunction(
(x: number, y: number): number => x + y,
);
func(3, 4);
func(4, 5);
isFunction()
export namespace functional {
export function isFunction<T extends (...args: any[]) => any>(
func: T,
): T extends (...args: infer Arguments) => infer Output
? Output extends Promise<infer R>
? (...args: Arguments) => Promise<R | null>
: (...args: Arguments) => Output | null
: never;
export function isParameters;
export function isReturn;
export function isEqualsFunction;
export function isEqualsParameters;
export function isEqualsReturn;
}
Tests a function.
typia.functional.isFunction<T>()
tests a function, by wrapping the parameter function and checking its paramters and return value through typia.is<T>()
function. If some parameter or return value does not match the expected type, it returns null
. Otherwise, it returns the return value of the parameter function.
By the way, if you don’t want to test both paramters and return value, but one of them, you can use typia.functional.isParameters<T>()
or typia.functional.isReturn<T>()
instead. Otherwise, if you want to prohibit superfluous properties, typia.functional.equalsFunction<T>()
would be helpful.
Also, if what you want is not just type checking, but want to know the detailed reason(s) why, utilize typia.functional.assertFunction<T>
() or typia.functional.validateFunction<T>()
instead.
import typia from "typia";
const func = typia.functional.isFunction(
(x: number, y: number): number => x + y,
);
func(3, 4);
func(4, 5);
Validates a function.
typia.functional.validateFunction<T>()
validates a function, by wrapping the parameter function and checking its paramters and return value through typia.validate<T>()
function. If some parameter or return value does not match the expected type, it returns a IValidation.IFailure
typed object. Otherwise, it returns a IValidation.ISuccess
typed object instead.
For reference, IValidation.IError.path
would be a little bit different with individual typia.validate<T>()
function. If IValidation.IError
occurs from some parameter, the path wouold start from $input.parameters[<index>]
. Otherwise the path would start from $input.return
.
$input.parameters[0].~
$input.return.~
By the way, if you don’t want to validate both paramters and return value, but one of them, you can use typia.functional.validateParameters<T>()
or typia.functional.validateReturn<T>()
instead. Otherwise, if you want to prohibit superfluous properties, typia.functional.validateEqualsFunction<T>()
would be helpful.
Also, if what you want is not retrieving every type errors, but just finding the first type error, utilize typia.functional.assertFunction<T>()
function instead. Otherwise, you don’t need the reason why, but just want to know whether the function is valid or not, use typia.functional.isFunction<T>()
function.
import typia from "typia";
const func = typia.functional.validateFunction(
(x: number, y: number): number => x + y,
);
func(3, 4);
func(4, 5);