runtype
exports type predicates—functions that take a value and validate it as a type.
The following list presents the available predicate methods. Note that several type utils are also exposed that might prove useful (like GuardedType
, to extract the type that a predicate guards) to you. These are not documented below, but can be imported from @codefeathers/runtype/util
, and have TSDoc accompanying them.
T(x)
:any(x)
x
is accepted as isF(x)
:never(x)
x
is nevernil(x)
:x
is null
or undefined
Null(x)
:x
is null
Undefined(x)
:x
is undefined
string(x)
:x
is a stringnumber(x)
:x
is a numberbool(x)
:x
is a boolsymbol(x)
:x
is a symbolobject(x)
:x
is a objectliteral(<LITERAL>)(x)
:equals(x)
<LITERAL>
is a valid string, number, bigint, boolean, or objectx
is equal to <LITERAL>
is(<X>)(x)
:<X>
is a valid constructorx
is an instanceof X
type(<NAME>)(x)
:<NAME>
is a string equal to one of the possible values of typeof
, or "null"
typeof x === <NAME>
(or) if <NAME>
is "null"
, x
is null
stringTag(<NAME>)(x)
:<NAME>
is a stringx[Symbol.toStringTag]
is equal to <NAME>
not(<f>)(x)
:<f>
is a Predicatex
does not satisfy predicate <f>
any
. Recommended to use exclude
if possibleexclude(<f>, <g>)(x)
:<f>
and <g>
are Predicatesx
is a member of the type represented by <f>
, but NOT a member of <g>
and(<fs>)(x)
:<fs>
is an array of Predicatesx
satisfies all of the Predicates in <fs>
refinement(<f>, <g>)(x)
:<f>
and <g>
are Predicatesx
satisfies the type represented by <f>
, and further the type represented by <g>
. Also: <f> & <g>
or(<fs>)(x)
:sum(<fs>)(x)
, union(<fs>)(x)
<fs>
is an array of Predicatesx
is a member of at least one of the Predicates in <fs>
either(<f>, <g>)(x)
:<f>
and <g>
are Predicatesx
satisfies either of the types represented by <f>
or <g>
maybe(<f>)(x)
:optional(<f>)(x)
<f>
is a Predicatex
either satisfies the type represented by <f>
, or is undefined
nullable(<f>)(x)
:<f>
is a Predicatex
either satisfies the type represented by <f>
, or is null
nilable(<f>)(x)
:<f>
is a Predicatex
either satisfies the type represented by <f>
, or is undefined | null
oneOf(<ys>)(x)
:<ys>
is an array of literals (string, number, bigint, boolean, or object)x
is exactly equal one of the given literalsproduct(<fs>)(x)
:tuple(<fs>)(x)
<fs>
is a tuple of Predicatesx
is a tuple whose members are represented by the types in <fs>
in orderarray(<f>)(xs)
:<f>
is a Predicatexs
is an array, where every member satisfies the type represented by <f>
struct(<struct>)(x)
:<struct>
is a JavaScript object, whose values are either nested structs, or Predicates. <struct>
may deeply nest as much as requiredx
is an object whose values satisfy the types provided by <struct>
’s vaues. x
must deeply nest in the same way <struct>
is to passextend(<f>, <struct>)(x)
:<f>
is a predicate guarding a Struct type; <struct>
is a JavaScript object, whose values are either nested structs, or Predicates. <struct>
may deeply nest as much as required<f>
and the struct <struct>
at runtimeThese are escape hatches designed to let you tell runtype
and subsequently TypeScript to trust you. Make sparing use of these types, and only when necessary. These are hidden under the namespace unsafe
.
import { unsafe } from "@codefeathers/runtype";
if (unsafe.own<string>(x)) {
// x is trusted as string
}
This can be useful when you have a complex or
type, or you have used not
, where you might lose type information and will have to tell TypeScript what type you know will come out of the assertion.
own<Type>(x)
:as<Type>(x)
<Type>
is a type parameter you must passruntype
ignores everything and asserts that x
is of type <Type>