Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/typeguardkit/mod.ts>NumberAsserter

A TypeScript module to help construct type assertion functions and type guards.
Latest
class NumberAsserter
implements Asserter<number>, Validator<number>
Re-export
import { NumberAsserter } from "https://deno.land/x/typeguardkit@0.33.0/mod.ts";

A NumberAsserter is an Asserter<number>, with any additional constraints defined by its NumberAsserterOptions properties.

The rules option can be used to specify validate functions and their requirements. Each validate function should return true if value is valid according to the rule, and false otherwise. requirements must not be empty or contain any blank strings.

The provided NumberAsserterOptions are made accessible as properties of the created NumberAsserter.

A NumberAsserter is also a Validator<number> with a validate method, which checks only that the provided value meets any constraints defined in the NumberAsserterOptions, and returns any issues. This can be used to validate user input client side, where it should already be known that value is a number.

Example:

import { Asserted, NumberAsserter } from "typeguardkit";

export const _EvenNumberInRange = new NumberAsserter("EvenNumberInRange", {
  min: { value: 0, inclusive: true },
  max: { value: 100, inclusive: true },
  step: 2,
});

export type EvenNumberInRange = Asserted<typeof _EvenNumberInRange>;

Constructors

new
NumberAsserter(typeName: string, unnamed 1: NumberAsserterOptions)

Properties

readonly
canBeNaN: boolean
readonly
max: NumberAsserterBound | null
readonly
min: NumberAsserterBound | null
readonly
rules: readonly NumberAsserterRule[]
readonly
step: number | null
readonly
typeName: string

Methods

assert(value: unknown, valueName?: string): number
validate(value: number): string[]