API reference › @evolu/common › Type › Number
const Number: createTypeOfType("Number");
Defined in: packages/common/src/Type.ts:2960
A JavaScript number, including NaN, Infinity, and -Infinity.
Use this Type directly only when those special values are meaningful. Most
numeric domains should exclude NaN with the nonNaN Type Factory or
the predefined NonNaNNumber. That is still insufficient for JSON and
other finite representations because it permits infinities; use
FiniteNumber at those boundaries.
Even FiniteNumber is usually only a foundation for domain constraints.
Int additionally requires a safely representable integer because
JavaScript numbers outside the safe integer range cannot preserve exact
integer identity. Add sign, range, and domain Brands as required.
Example
An age is a non-negative safe integer below 200:
import {
assertEqual,
assertErr,
assertOk,
assertType,
Age,
Data,
FiniteNumber,
Int,
NonNaNNumber,
NonNegativeInt,
Number,
type Brand,
} from "@evolu/common";
// Note how every additional constraint accumulates its Brand.
assertType<number, typeof Number.Output>();
assertType<number & Brand<"NonNaN">, typeof NonNaNNumber.Output>();
assertType<
number & Brand<"NonNaN"> & Brand<"Finite">,
typeof FiniteNumber.Output
>();
assertType<
number & Brand<"NonNaN"> & Brand<"Finite"> & Brand<"Int">,
typeof Int.Output
>();
assertType<
number &
Brand<"NonNaN"> &
Brand<"Finite"> &
Brand<"Int"> &
Brand<"NonNegative">,
typeof NonNegativeInt.Output
>();
assertType<
number &
Brand<"NonNaN"> &
Brand<"Finite"> &
Brand<"Int"> &
Brand<"NonNegative"> &
Brand<"LessThan200"> &
Brand<"Age">,
typeof Age.Output
>();
assertOk(Age.fromUnknown(122), 122);
const invalid = Age.fromUnknown(200);
assertErr(invalid);
assertType(Data, invalid.error);
assertEqual(invalid.error, {
type: "LessThan200",
value: 200,
max: 200,
});