CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-sinclair--typebox

Json Schema Type Builder with Static Type Resolution for TypeScript

Pending
Overview
Eval results
Files

basic-types.mddocs/

Basic Types

Core JSON Schema compatible types that form the foundation of all TypeBox schemas. These types provide primitive and structured data validation with full TypeScript inference.

Capabilities

String Type

Creates a string type with optional validation constraints.

/**
 * Creates a string type with optional validation constraints
 * @param options - String validation options
 * @returns TString schema
 */
function String(options?: StringOptions): TString;

interface StringOptions extends SchemaOptions {
  minLength?: number;
  maxLength?: number;
  pattern?: string;
  format?: 'date-time' | 'date' | 'time' | 'email' | 'hostname' | 'ipv4' | 'ipv6' | 'uri' | 'uuid' | string;
}

Usage Examples:

import { Type } from "@sinclair/typebox";

// Basic string
const Name = Type.String();

// String with length constraints
const Username = Type.String({ minLength: 3, maxLength: 20 });

// String with pattern validation
const PhoneNumber = Type.String({ pattern: '^\\+?[1-9]\\d{1,14}$' });

// String with format validation
const Email = Type.String({ format: 'email' });
const UUID = Type.String({ format: 'uuid' });

Number Type

Creates a number type with optional validation constraints.

/**
 * Creates a number type with optional validation constraints
 * @param options - Number validation options
 * @returns TNumber schema
 */
function Number(options?: NumberOptions): TNumber;

interface NumberOptions extends SchemaOptions {
  minimum?: number;
  maximum?: number;
  exclusiveMinimum?: number;
  exclusiveMaximum?: number;
  multipleOf?: number;
}

Usage Examples:

// Basic number
const Age = Type.Number();

// Number with range constraints
const Score = Type.Number({ minimum: 0, maximum: 100 });

// Number with exclusive bounds
const Temperature = Type.Number({ exclusiveMinimum: -273.15 });

// Number with multiple constraint
const EvenNumber = Type.Number({ multipleOf: 2 });

Integer Type

Creates an integer type with optional validation constraints.

/**
 * Creates an integer type with optional validation constraints
 * @param options - Integer validation options
 * @returns TInteger schema
 */
function Integer(options?: NumberOptions): TInteger;

Usage Examples:

// Basic integer
const Count = Type.Integer();

// Integer with range
const Port = Type.Integer({ minimum: 1, maximum: 65535 });

Boolean Type

Creates a boolean type.

/**
 * Creates a boolean type
 * @param options - Schema options
 * @returns TBoolean schema
 */
function Boolean(options?: SchemaOptions): TBoolean;

Usage Examples:

// Basic boolean
const IsActive = Type.Boolean();

// Boolean with description
const HasPermission = Type.Boolean({ description: 'User has admin permissions' });

Array Type

Creates an array type with item validation.

/**
 * Creates an array type with item validation
 * @param items - Schema for array items
 * @param options - Array validation options
 * @returns TArray schema
 */
function Array<T extends TSchema>(items: T, options?: ArrayOptions): TArray<T>;

interface ArrayOptions extends SchemaOptions {
  minItems?: number;
  maxItems?: number;
  uniqueItems?: boolean;
  contains?: TSchema;
  minContains?: number;
  maxContains?: number;
}

Usage Examples:

// Array of strings
const Tags = Type.Array(Type.String());

// Array with length constraints
const Items = Type.Array(Type.String(), { minItems: 1, maxItems: 10 });

// Array with unique items
const UniqueNumbers = Type.Array(Type.Number(), { uniqueItems: true });

// Array of objects
const Users = Type.Array(Type.Object({
  name: Type.String(),
  age: Type.Number()
}));

Object Type

Creates an object type with property schemas.

/**
 * Creates an object type with property schemas
 * @param properties - Object property schemas
 * @param options - Object validation options
 * @returns TObject schema
 */
function Object<T extends TProperties>(properties: T, options?: ObjectOptions): TObject<T>;

interface ObjectOptions extends SchemaOptions {
  additionalProperties?: boolean;
  minProperties?: number;
  maxProperties?: number;
}

Usage Examples:

// Basic object
const Person = Type.Object({
  name: Type.String(),
  age: Type.Number(),
  email: Type.String({ format: 'email' })
});

// Object with optional properties
const User = Type.Object({
  id: Type.String(),
  name: Type.String(),
  bio: Type.Optional(Type.String())
});

// Object without additional properties
const StrictUser = Type.Object({
  name: Type.String(),
  age: Type.Number()
}, { additionalProperties: false });

Union Type

Creates a union of multiple types (OR logic).

/**
 * Creates a union of multiple types (OR logic)
 * @param types - Array of schemas to union
 * @param options - Schema options
 * @returns Union schema
 */
function Union<Types extends TSchema[]>(types: [...Types], options?: SchemaOptions): Union<Types>;

Usage Examples:

// String or number
const StringOrNumber = Type.Union([Type.String(), Type.Number()]);

// Multiple object types
const AdminOrUser = Type.Union([
  Type.Object({ role: Type.Literal('admin'), permissions: Type.Array(Type.String()) }),
  Type.Object({ role: Type.Literal('user'), profile: Type.String() })
]);

// Nullable type
const NullableString = Type.Union([Type.String(), Type.Null()]);

Literal Type

Creates a literal type for exact value matching.

/**
 * Creates a literal type for exact value matching
 * @param value - The literal value
 * @returns TLiteral schema
 */
function Literal<T extends TLiteralValue>(value: T): TLiteral<T>;

type TLiteralValue = string | number | boolean;

Usage Examples:

// String literal
const Status = Type.Literal('active');

// Number literal
const Version = Type.Literal(1);

// Boolean literal
const Enabled = Type.Literal(true);

// Union of literals (enum-like)
const Color = Type.Union([
  Type.Literal('red'),
  Type.Literal('green'),
  Type.Literal('blue')
]);

Enum Type

Creates an enum type from object keys/values.

/**
 * Creates an enum type from object keys/values
 * @param item - Enum object with string or number values
 * @param options - Schema options
 * @returns TEnum schema
 */
function Enum<V extends string | number, T extends Record<string, V>>(item: T, options?: SchemaOptions): TEnum<T>;

Usage Examples:

// Enum from object
enum Color {
  Red = 'red',
  Green = 'green',
  Blue = 'blue'
}
const ColorEnum = Type.Enum(Color);

// Enum from numeric enum
enum Status {
  Pending = 0,
  Approved = 1,
  Rejected = 2
}
const StatusEnum = Type.Enum(Status);

Null and Undefined

Creates null and undefined types.

/**
 * Creates a null type
 * @param options - Schema options
 * @returns TNull schema
 */
function Null(options?: SchemaOptions): TNull;

/**
 * Creates an undefined type
 * @param options - Schema options  
 * @returns TUndefined schema
 */
function Undefined(options?: SchemaOptions): TUndefined;

Tuple Type

Creates a tuple type with fixed element types and order.

/**
 * Creates a tuple type with fixed element types and order
 * @param items - Array of schemas for tuple elements
 * @param options - Schema options
 * @returns TTuple schema
 */
function Tuple<T extends TSchema[]>(items: [...T], options?: SchemaOptions): TTuple<T>;

Usage Examples:

// Basic tuple
const Point2D = Type.Tuple([Type.Number(), Type.Number()]);
// Type: [number, number]

// Mixed type tuple
const UserRecord = Type.Tuple([
  Type.String(), // id
  Type.String(), // name  
  Type.Number()  // age
]);
// Type: [string, string, number]

// Tuple with different types
const ApiResponse = Type.Tuple([
  Type.Number(),  // status code
  Type.String(),  // message
  Type.Any()      // data
]);

Const Type

Creates a const type for deeply immutable literal values.

/**
 * Creates a const type for deeply immutable literal values
 * @param value - The const value
 * @param options - Schema options
 * @returns TConst schema
 */
function Const<T extends TLiteralValue | object | any[]>(value: T, options?: SchemaOptions): TConst<T>;

Usage Examples:

// Const primitive
const Status = Type.Const('active');

// Const object
const Config = Type.Const({
  version: 1,
  enabled: true,
  features: ['auth', 'logging']
});

// Const array
const DefaultTags = Type.Const(['user', 'active']);

Any, Unknown, Never

Special utility types for flexible validation.

/**
 * Creates an Any type that accepts all values
 * @param options - Schema options
 * @returns TAny schema
 */
function Any(options?: SchemaOptions): TAny;

/**
 * Creates an Unknown type (accepts all but requires type checking)
 * @param options - Schema options
 * @returns TUnknown schema
 */
function Unknown(options?: SchemaOptions): TUnknown;

/**
 * Creates a Never type (no valid values)
 * @param options - Schema options
 * @returns TNever schema
 */
function Never(options?: SchemaOptions): TNever;

Type Interfaces

interface TString extends TSchema {
  type: 'string';
  minLength?: number;
  maxLength?: number;
  pattern?: string;
  format?: string;
}

interface TNumber extends TSchema {
  type: 'number';
  minimum?: number;
  maximum?: number;
  exclusiveMinimum?: number;
  exclusiveMaximum?: number;
  multipleOf?: number;
}

interface TInteger extends TSchema {
  type: 'integer';
  minimum?: number;
  maximum?: number;
  exclusiveMinimum?: number;
  exclusiveMaximum?: number;
  multipleOf?: number;
}

interface TBoolean extends TSchema {
  type: 'boolean';
}

interface TArray<T extends TSchema> extends TSchema {
  type: 'array';
  items: T;
  minItems?: number;
  maxItems?: number;
  uniqueItems?: boolean;
}

interface TObject<T extends TProperties> extends TSchema {
  type: 'object';
  properties: T;
  required?: string[];
  additionalProperties?: boolean;
  minProperties?: number;
  maxProperties?: number;
}

interface TUnion<T extends TSchema[]> extends TSchema {
  anyOf: T;
}

interface TLiteral<T extends TLiteralValue> extends TSchema {
  const: T;
}

interface TEnum<T> extends TSchema {
  enum: T extends readonly any[] ? T : T extends Record<string, infer U> ? U[] : never;
}

interface TNull extends TSchema {
  type: 'null';
}

interface TUndefined extends TSchema {
  not: {};
}

interface TAny extends TSchema {
  // No type constraint
}

interface TUnknown extends TSchema {
  // No type constraint
}

interface TNever extends TSchema {
  not: {};
}

interface TTuple<T extends TSchema[]> extends TSchema {
  type: 'array';
  items: T;
  minItems: T['length'];
  maxItems: T['length'];
  additionalItems: false;
}

interface TConst<T> extends TSchema {
  const: T;
}

interface TVoid extends TSchema {
  type: 'null';
}

Install with Tessl CLI

npx tessl i tessl/npm-sinclair--typebox

docs

additional-namespaces.md

advanced-types.md

basic-types.md

compilation.md

index.md

javascript-types.md

transforms.md

value-operations.md

tile.json