TypeScript runtime type system for IO decoding/encoding
72
Built-in codecs for basic JavaScript/TypeScript types with runtime validation.
Validates that a value is a string.
/**
* String codec that validates string values
*/
const string: StringC;
interface StringC extends Type<string, string, unknown> {}Usage Example:
import * as t from "io-ts";
const result = t.string.decode("hello");
// result: Right("hello")
const invalid = t.string.decode(42);
// result: Left([validation error])Validates that a value is a number (excluding NaN).
/**
* Number codec that validates number values
*/
const number: NumberC;
interface NumberC extends Type<number, number, unknown> {}Usage Example:
import * as t from "io-ts";
const result = t.number.decode(42);
// result: Right(42)
const invalid = t.number.decode("42");
// result: Left([validation error])Validates that a value is a boolean.
/**
* Boolean codec that validates boolean values
*/
const boolean: BooleanC;
interface BooleanC extends Type<boolean, boolean, unknown> {}Validates that a value is a bigint.
/**
* BigInt codec that validates bigint values
*/
const bigint: BigIntC;
interface BigIntC extends Type<bigint, bigint, unknown> {}Codecs for null and undefined values.
/**
* Null codec that validates null values
*/
const nullType: NullC;
const null: NullC; // exported alias
interface NullC extends Type<null, null, unknown> {}
/**
* Undefined codec that validates undefined values
*/
const undefined: UndefinedC;
interface UndefinedC extends Type<undefined, undefined, unknown> {}
/**
* Void codec that validates void/undefined values
*/
const voidType: VoidC;
const void: VoidC; // exported alias
interface VoidC extends Type<void, void, unknown> {}Codecs that handle any or no values.
/**
* Unknown codec that accepts any value
*/
const unknown: UnknownC;
interface UnknownC extends Type<unknown, unknown, unknown> {}
/**
* Any codec that accepts any value (deprecated, use unknown)
*/
const any: AnyC;
interface AnyC extends Type<any, any, unknown> {}
/**
* Never codec that always fails validation
*/
const never: NeverC;
interface NeverC extends Type<never, never, unknown> {}Validates that a value is a function.
/**
* Function codec that validates function values
*/
const Function: FunctionC;
interface FunctionC extends Type<Function, Function, unknown> {}Generic container codecs for arrays and objects.
/**
* Unknown array codec that validates arrays of unknown elements
*/
const UnknownArray: UnknownArrayC;
interface UnknownArrayC extends Type<Array<unknown>, Array<unknown>, unknown> {}
/**
* Unknown record codec that validates objects with string keys and unknown values
*/
const UnknownRecord: UnknownRecordC;
interface UnknownRecordC extends Type<{ [key: string]: unknown }, { [key: string]: unknown }, unknown> {}Usage Examples:
import * as t from "io-ts";
// Array validation
const arrayResult = t.UnknownArray.decode([1, 2, "hello"]);
// result: Right([1, 2, "hello"])
// Record validation
const recordResult = t.UnknownRecord.decode({ a: 1, b: "hello" });
// result: Right({ a: 1, b: "hello" })Branded integer codec for validating integer numbers.
/**
* Integer codec that validates integer numbers (branded number)
*/
const Int: BrandC<NumberC, IntBrand>;
interface IntBrand {
readonly Int: unique symbol;
}
type Int = Branded<number, IntBrand>;Usage Example:
import * as t from "io-ts";
const result = t.Int.decode(42);
// result: Right(42) with Int brand
const invalid = t.Int.decode(42.5);
// result: Left([validation error - not an integer])These primitives are maintained for backward compatibility but should be avoided in new code:
/**
* @deprecated Use UnknownArray instead
*/
const Array: UnknownArrayC;
/**
* @deprecated Use UnknownRecord instead
*/
const Dictionary: UnknownRecordC;
/**
* @deprecated Use UnknownRecord instead
*/
const object: ObjectC;
/**
* @deprecated Use unknown instead
*/
type mixed = unknown;import * as t from "io-ts";
import { PathReporter } from "io-ts/PathReporter";
const validatePrimitives = (data: unknown[]) => {
data.forEach((item, index) => {
console.log(`Item ${index}:`, item);
// Test against different primitive codecs
const tests = [
{ codec: t.string, name: 'string' },
{ codec: t.number, name: 'number' },
{ codec: t.boolean, name: 'boolean' },
{ codec: t.unknown, name: 'unknown' }
];
tests.forEach(({ codec, name }) => {
const result = codec.decode(item);
if (result._tag === 'Right') {
console.log(` ✓ Valid ${name}`);
} else {
console.log(` ✗ Invalid ${name}:`, PathReporter.failure(result.left));
}
});
});
};
validatePrimitives([42, "hello", true, null, undefined]);import * as t from "io-ts";
// Use built-in type guards
const value: unknown = "hello";
if (t.string.is(value)) {
// value is now typed as string
console.log(value.toUpperCase());
}
if (t.number.is(value)) {
// This block won't execute
console.log(value.toFixed(2));
}import * as t from "io-ts";
// Create a union of primitives
const StringOrNumber = t.union([t.string, t.number]);
const result1 = StringOrNumber.decode("hello");
// result: Right("hello")
const result2 = StringOrNumber.decode(42);
// result: Right(42)
const result3 = StringOrNumber.decode(true);
// result: Left([validation error])Install with Tessl CLI
npx tessl i tessl/npm-io-tsdocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10