runtype

runtype API

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.

Table of contents

Always

T(x):

aliases: any(x)

F(x):

aliases: never(x)

Primitives

nil(x):

Null(x):

Undefined(x):

string(x):

number(x):

bool(x):

symbol(x):

object(x):

literal(<LITERAL>)(x):

aliases: equals(x)

is(<X>)(x):

type(<NAME>)(x):

stringTag(<NAME>)(x):

Type combiners

not(<f>)(x):

exclude(<f>, <g>)(x):

and(<fs>)(x):

refinement(<f>, <g>)(x):

or(<fs>)(x):

aliases: sum(<fs>)(x), union(<fs>)(x)

either(<f>, <g>)(x):

maybe(<f>)(x):

aliases: optional(<f>)(x)

nullable(<f>)(x):

nilable(<f>)(x):

oneOf(<ys>)(x):

product(<fs>)(x):

aliases: tuple(<fs>)(x)

array(<f>)(xs):

struct(<struct>)(x):

extend(<f>, <struct>)(x):

Unsafe

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.

own<Type>(x):

aliases: as<Type>(x)