Runtime validation for static types
—
Core type validators for JavaScript primitive values and special types. These are the building blocks for more complex validation schemas.
Validates that a value is a string.
/**
* Validates that a value is a string
* @example String.check("hello") // "hello"
* @example String.guard(123) // false
*/
const String: Runtype<string>;Usage Examples:
import { String } from "runtypes";
// Basic validation
const name = String.check("Alice"); // "Alice"
String.check(123); // throws ValidationError
// Type guard usage
function processName(input: unknown) {
if (String.guard(input)) {
// input is now typed as string
return input.toUpperCase();
}
throw new Error("Expected string");
}Validates that a value is a number.
/**
* Validates that a value is a number
* @example Number.check(42) // 42
* @example Number.guard("42") // false
*/
const Number: Runtype<number>;Usage Examples:
import { Number } from "runtypes";
// Validation
const age = Number.check(25); // 25
const price = Number.parse("invalid"); // throws ValidationError
// With constraints
const PositiveNumber = Number.withConstraint(n => n > 0 || "Must be positive");
const count = PositiveNumber.check(5); // 5Validates that a value is a boolean.
/**
* Validates that a value is a boolean
* @example Boolean.check(true) // true
* @example Boolean.guard("true") // false
*/
const Boolean: Runtype<boolean>;Validates that a value is a bigint.
/**
* Validates that a value is a bigint
* @example BigInt.check(123n) // 123n
* @example BigInt.guard(123) // false
*/
const BigInt: Runtype<bigint>;Usage Examples:
import { BigInt } from "runtypes";
const largeNumber = BigInt.check(9007199254740991n);
// For parsing from strings, combine with Parser
const BigIntFromString = BigInt.withParser(str => globalThis.BigInt(str));Validates that a value is a symbol. Can optionally validate specific symbol descriptions.
/**
* Validates that a value is a symbol
* @example Symbol.check(Symbol("test")) // Symbol(test)
* @example Symbol("test").check(Symbol("test")) // Symbol(test)
*/
const Symbol: Runtype<symbol> & ((description?: string) => Runtype<symbol>);Usage Examples:
import { Symbol } from "runtypes";
// Any symbol
const anySymbol = Symbol.check(Symbol("test"));
// Specific symbol description
const testSymbol = Symbol("test");
testSymbol.check(Symbol("test")); // passes
testSymbol.check(Symbol("other")); // throws ValidationErrorValidates that a value is exactly null.
/**
* Validates that a value is exactly null
* @example Null.check(null) // null
* @example Null.guard(undefined) // false
*/
const Null: Runtype<null>;Validates that a value is exactly undefined.
/**
* Validates that a value is exactly undefined
* @example Undefined.check(undefined) // undefined
* @example Undefined.guard(null) // false
*/
const Undefined: Runtype<undefined>;Alias for Undefined, commonly used for function return types.
/**
* Validates that a value is exactly undefined (alias for Undefined)
* @example Void.check(undefined) // undefined
*/
const Void: Runtype<undefined>;Accepts any value (top type). Useful as a starting point for validation chains.
/**
* Accepts any value (top type)
* @example Unknown.check(anything) // anything
*/
const Unknown: Runtype<unknown>;Usage Examples:
import { Unknown, String } from "runtypes";
// Accept any input, then narrow down
const validateInput = (input: unknown) => {
Unknown.check(input); // Always passes
if (String.guard(input)) {
return { type: "string", value: input };
}
return { type: "other", value: input };
};Rejects all values (bottom type). Useful for impossible cases and strict validation.
/**
* Rejects all values (bottom type)
* @example Never.check(anything) // always throws ValidationError
*/
const Never: Runtype<never>;Usage Examples:
import { Never, Object } from "runtypes";
// Reject extra properties in exact objects
const StrictUser = Object({
name: String,
age: Number
}).exact(); // Uses Never internally for extra propertiesValidates that a value is null or undefined (nullish values).
/**
* Validates that a value is null or undefined
* @example Nullish.check(null) // null
* @example Nullish.check(undefined) // undefined
* @example Nullish.guard("") // false
*/
const Nullish: Runtype<null | undefined>;Usage Examples:
import { Nullish, String, Union } from "runtypes";
// Optional string that can be nullish
const OptionalString = Union(String, Nullish);
// Or using the nullable/undefinedable methods
const NullableString = String.nullable(); // string | null
const UndefinableString = String.undefinedable(); // string | undefined
const NullishString = String.nullishable(); // string | null | undefinedimport { String, Number, Boolean, Union, Object } from "runtypes";
// Mixed primitive validation
const MixedData = Object({
id: Union(String, Number), // string or number
name: String,
active: Boolean,
tags: Array(String),
metadata: Unknown.optional() // any value, optional
});
type MixedDataType = Static<typeof MixedData>;
// {
// id: string | number;
// name: string;
// active: boolean;
// tags: string[];
// metadata?: unknown;
// }Install with Tessl CLI
npx tessl i tessl/npm-runtypes