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.
- [asserts] type of
x
is accepted as is
- [asserts]
x
is never
- [asserts]
x
isnull
orundefined
- [asserts]
x
isnull
- [asserts]
x
isundefined
- [asserts]
x
is a string
- [asserts]
x
is a number
- [asserts]
x
is a bool
- [asserts]
x
is a symbol
- [asserts]
x
is a object
- [where]
<LITERAL>
is a valid string, number, bigint, boolean, or object - [asserts]
x
is equal to<LITERAL>
- [where]
<X>
is a valid constructor - [asserts]
x
is an instanceofX
- [where]
<NAME>
is a string equal to one of the possible values oftypeof
, or"null"
- [asserts]
typeof x === <NAME>
(or) if<NAME>
is"null"
,x
isnull
- [where]
<NAME>
is a string - [asserts]
x[Symbol.toStringTag]
is equal to<NAME>
- [where]
<f>
is a Predicate - [asserts]
x
does not satisfy predicate<f>
- [warning] The type-guard for this method is not accurate. Will always default to
any
. Recommended to useexclude
if possible
- [where]
<f>
and<g>
are Predicates - [asserts]
x
is a member of the type represented by<f>
, but NOT a member of<g>
- [where]
<fs>
is an array of Predicates - [asserts]
x
satisfies all of the Predicates in<fs>
- [where]
<f>
and<g>
are Predicates - [asserts]
x
satisfies the type represented by<f>
, and further the type represented by<g>
. Also:<f> & <g>
- [where]
<fs>
is an array of Predicates - [asserts]
x
is a member of at least one of the Predicates in<fs>
- [where]
<f>
and<g>
are Predicates - [asserts]
x
satisfies either of the types represented by<f>
or<g>
- [where]
<f>
is a Predicate - [asserts]
x
either satisfies the type represented by<f>
, or isundefined
- [where]
<f>
is a Predicate - [asserts]
x
either satisfies the type represented by<f>
, or isnull
- [where]
<f>
is a Predicate - [asserts]
x
either satisfies the type represented by<f>
, or isundefined | null
- [where]
<ys>
is an array of literals (string, number, bigint, boolean, or object) - [asserts]
x
is exactly equal one of the given literals
- [where]
<fs>
is a tuple of Predicates - [asserts]
x
is a tuple whose members are represented by the types in<fs>
in order
- [where]
<f>
is a Predicate - [asserts]
xs
is an array, where every member satisfies the type represented by<f>
- [where]
<struct>
is a JavaScript object, whose values are either nested structs, or Predicates.<struct>
may deeply nest as much as required - [asserts]
x
is an object whose values satisfy the types provided by<struct>
's vaues.x
must deeply nest in the same way<struct>
is to pass
- [where]
<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 - [asserts] x is validated against the predicate's type at compile time, and validated against both the predicate
<f>
and the struct<struct>
at runtime
These 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.
- [where]
<Type>
is a type parameter you must pass - [asserts]
runtype
ignores everything and asserts thatx
is of type<Type>