🔍
verifica
  • Verifica
  • Getting started
    • 📦Installation
    • 🧑‍🎓 Examples
      • 🧑‍🎓 Basic
      • 🧑‍🎓 Multiple conditions
      • 🧑‍🎓 Optional
      • 🧑‍🎓 Custom rules
      • 🧑‍🎓 With express.js
    • 🟦Typescript
  • Functions
    • asVerificable
    • isValid
    • getErrors
    • ensure
    • rawValue
  • Predicates
    • Built-in Predicates
      • isArray
      • isArrayLength
      • isArrayOf
      • isBoolean
      • isFiniteNumber
      • isInteger
      • isNumber
      • isNumberInRange
      • isObject
      • isOneOf
      • isRegexMatch
      • isString
      • isStringLength
    • Operations on Predicates
      • all
      • optional
    • Custom Predicates
Powered by GitBook
On this page

Was this helpful?

  1. Getting started

Typescript

Verifica comes with type definitions, so no additional packages are needed.

ensure()

function ensure(verificable: Verificable, predicate: Predicate<TOut>): TOut

Function 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;
}

Previous🧑‍🎓 With express.jsNextFunctions

Last updated 5 years ago

Was this helpful?

🟦