Json Schema Type Builder with Static Type Resolution for TypeScript
—
Types that represent JavaScript runtime constructs like functions, constructors, dates, and other built-in objects. These types extend JSON Schema to support JavaScript-specific validation scenarios.
Creates a function type with parameter and return type validation.
/**
* Creates a function type with parameter and return type validation
* @param parameters - Array of parameter schemas
* @param returns - Return value schema
* @returns TFunction schema
*/
function Function<P extends TSchema[], R extends TSchema>(parameters: [...P], returns: R): TFunction<P, R>;Usage Examples:
import { Type } from "@sinclair/typebox";
// Simple function: (x: number, y: number) => number
const AddFunction = Type.Function([Type.Number(), Type.Number()], Type.Number());
// Function with optional parameters
const LogFunction = Type.Function([
Type.String(),
Type.Optional(Type.Object({ level: Type.String() }))
], Type.Void());
// Async function returning Promise
const FetchFunction = Type.Function([Type.String()], Type.Promise(Type.Object({
status: Type.Number(),
data: Type.Any()
})));Creates a constructor function type.
/**
* Creates a constructor function type
* @param parameters - Array of constructor parameter schemas
* @param returns - Instance type schema
* @returns TConstructor schema
*/
function Constructor<P extends TSchema[], R extends TSchema>(parameters: [...P], returns: R): TConstructor<P, R>;Usage Examples:
// Constructor for a User class
const UserConstructor = Type.Constructor([
Type.String(), // name
Type.Number() // age
], Type.Object({
name: Type.String(),
age: Type.Number(),
id: Type.String()
}));Extracts parameter and return types from functions and constructors.
/**
* Extracts constructor parameters from Constructor type
* @param schema - Constructor schema
* @returns TConstructorParameters schema
*/
function ConstructorParameters<T extends TConstructor>(schema: T): TConstructorParameters<T>;
/**
* Extracts instance type from Constructor type
* @param schema - Constructor schema
* @returns TInstanceType schema
*/
function InstanceType<T extends TConstructor>(schema: T): TInstanceType<T>;
/**
* Extracts parameters from Function type
* @param schema - Function schema
* @returns TParameters schema
*/
function Parameters<T extends TFunction>(schema: T): TParameters<T>;
/**
* Extracts return type from Function type
* @param schema - Function schema
* @returns TReturnType schema
*/
function ReturnType<T extends TFunction>(schema: T): TReturnType<T>;Creates a Date type with timestamp validation.
/**
* Creates a Date type with timestamp validation
* @param options - Date validation options
* @returns TDate schema
*/
function Date(options?: DateOptions): TDate;
interface DateOptions extends SchemaOptions {
minimum?: number | string | Date;
maximum?: number | string | Date;
exclusiveMinimum?: number | string | Date;
exclusiveMaximum?: number | string | Date;
}Usage Examples:
// Basic date
const CreatedAt = Type.Date();
// Date with range constraints
const FutureDate = Type.Date({
minimum: new Date().toISOString()
});
// Date with both bounds
const EventDate = Type.Date({
minimum: '2024-01-01T00:00:00Z',
maximum: '2024-12-31T23:59:59Z'
});Creates a Promise type with inner type validation.
/**
* Creates a Promise type with inner type validation
* @param item - Schema for the resolved value
* @returns TPromise schema
*/
function Promise<T extends TSchema>(item: T): TPromise<T>;Usage Examples:
// Promise that resolves to string
const StringPromise = Type.Promise(Type.String());
// Promise that resolves to object
const UserPromise = Type.Promise(Type.Object({
id: Type.String(),
name: Type.String()
}));
// Promise that resolves to array
const ItemsPromise = Type.Promise(Type.Array(Type.String()));Unwraps Promise types recursively.
/**
* Unwraps Promise types recursively
* @param schema - Promise schema to unwrap
* @returns TAwaited schema
*/
function Awaited<T extends TSchema>(schema: T): TAwaited<T>;Usage Examples:
const NestedPromise = Type.Promise(Type.Promise(Type.String()));
const UnwrappedType = Type.Awaited(NestedPromise);
// Result: string (fully unwrapped)Creates a RegExp type for pattern matching.
/**
* Creates a RegExp type for pattern matching
* @param source - Regular expression source
* @param flags - Regular expression flags
* @returns TRegExp schema
*/
function RegExp(source: string, flags?: string): TRegExp;Usage Examples:
// Email pattern
const EmailRegex = Type.RegExp('^[\\w-\\.]+@[\\w-]+\\.[a-z]{2,}$', 'i');
// Phone number pattern
const PhoneRegex = Type.RegExp('^\\+?[1-9]\\d{1,14}$');
// UUID pattern
const UuidRegex = Type.RegExp('^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$', 'i');Creates a BigInt type for large integer values.
/**
* Creates a BigInt type
* @param options - BigInt validation options
* @returns TBigInt schema
*/
function BigInt(options?: BigIntOptions): TBigInt;
interface BigIntOptions extends SchemaOptions {
minimum?: bigint;
maximum?: bigint;
exclusiveMinimum?: bigint;
exclusiveMaximum?: bigint;
multipleOf?: bigint;
}Usage Examples:
// Basic BigInt
const LargeNumber = Type.BigInt();
// BigInt with constraints
const PositiveBigInt = Type.BigInt({ minimum: 0n });
// BigInt with range
const TimestampBigInt = Type.BigInt({
minimum: 0n,
maximum: 9223372036854775807n // Max safe BigInt
});Creates a Symbol type for unique identifiers.
/**
* Creates a Symbol type
* @param options - Schema options
* @returns TSymbol schema
*/
function Symbol(options?: SchemaOptions): TSymbol;Usage Examples:
// Basic symbol
const UniqueKey = Type.Symbol();
// Symbol with description
const EventSymbol = Type.Symbol({
description: 'Unique event identifier'
});Creates a Uint8Array type for binary data.
/**
* Creates a Uint8Array type with byte length validation
* @param options - Uint8Array validation options
* @returns TUint8Array schema
*/
function Uint8Array(options?: Uint8ArrayOptions): TUint8Array;
interface Uint8ArrayOptions extends SchemaOptions {
minByteLength?: number;
maxByteLength?: number;
}Usage Examples:
// Basic Uint8Array
const BinaryData = Type.Uint8Array();
// Uint8Array with length constraints
const Hash = Type.Uint8Array({
minByteLength: 32,
maxByteLength: 32
});
// Small buffer
const SmallBuffer = Type.Uint8Array({ maxByteLength: 1024 });Creates Iterator and AsyncIterator types.
/**
* Creates an Iterator type
* @param items - Schema for iterator values
* @returns TIterator schema
*/
function Iterator<T extends TSchema>(items: T): TIterator<T>;
/**
* Creates an AsyncIterator type
* @param items - Schema for async iterator values
* @returns TAsyncIterator schema
*/
function AsyncIterator<T extends TSchema>(items: T): TAsyncIterator<T>;Usage Examples:
// Iterator of strings
const StringIterator = Type.Iterator(Type.String());
// AsyncIterator of objects
const UserIterator = Type.AsyncIterator(Type.Object({
id: Type.String(),
name: Type.String()
}));Creates a Void type (undefined or null based on policy).
/**
* Creates a Void type
* @param options - Schema options
* @returns TVoid schema
*/
function Void(options?: SchemaOptions): TVoid;Usage Examples:
// Basic void (for function returns)
const VoidReturn = Type.Void();
// Function that returns void
const LoggerFunction = Type.Function([Type.String()], Type.Void());interface TFunction<P extends TSchema[], R extends TSchema> extends TSchema {
type: 'function';
parameters: P;
returns: R;
}
interface TConstructor<P extends TSchema[], R extends TSchema> extends TSchema {
type: 'constructor';
parameters: P;
returns: R;
}
interface TDate extends TSchema {
type: 'date';
minimum?: number | string;
maximum?: number | string;
exclusiveMinimum?: number | string;
exclusiveMaximum?: number | string;
}
interface TPromise<T extends TSchema> extends TSchema {
type: 'promise';
item: T;
}
interface TRegExp extends TSchema {
type: 'regexp';
source: string;
flags?: string;
}
interface TBigInt extends TSchema {
type: 'bigint';
minimum?: bigint;
maximum?: bigint;
exclusiveMinimum?: bigint;
exclusiveMaximum?: bigint;
multipleOf?: bigint;
}
interface TSymbol extends TSchema {
type: 'symbol';
}
interface TUint8Array extends TSchema {
type: 'uint8array';
minByteLength?: number;
maxByteLength?: number;
}
interface TIterator<T extends TSchema> extends TSchema {
type: 'iterator';
items: T;
}
interface TAsyncIterator<T extends TSchema> extends TSchema {
type: 'async-iterator';
items: T;
}
interface TVoid extends TSchema {
type: 'void';
}
// Utility type interfaces
interface TConstructorParameters<T extends TConstructor> extends TSchema {
type: 'tuple';
items: T['parameters'];
}
interface TInstanceType<T extends TConstructor> extends TSchema {
[Kind]: 'InstanceType';
type: T['returns']['type'];
}
interface TParameters<T extends TFunction> extends TSchema {
type: 'tuple';
items: T['parameters'];
}
interface TReturnType<T extends TFunction> extends TSchema {
[Kind]: 'ReturnType';
type: T['returns']['type'];
}
interface TAwaited<T extends TSchema> extends TSchema {
[Kind]: 'Awaited';
// Recursively unwraps Promise types
}Install with Tessl CLI
npx tessl i tessl/npm-sinclair--typebox