π¦Typescript
Verifica comes with type definitions, so no additional packages are needed.
ensure()
function ensure(verificable: Verificable, predicate: Predicate<TOut>): TOutFunction ensure returns the checked value if the predicate matches. The type of returned value is the same as the generic parameter type of the Predicate<TOut> . This allows to use the value with the correct type after ensuring it matches the predicate:
import { asVerificable, ensure, isNumber } from "verifica";
function add(a: unknown, b: unknown): number {
const vparams = asVerificable({ a, b });
// both `aNum` and `bNum` will be of type `string`:
const aNum = ensure(vparams.a, isNumber);
const bNum = ensure(vparams.b, isNumber);
return aNum + bNum;
}Last updated
Was this helpful?