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

advanced-types.mddocs/

Advanced Types

TypeScript-inspired advanced types including utility types, conditional types, and template literals for complex schema composition. These types enable sophisticated type transformations and constraints.

Capabilities

Utility Types

Partial Type

Makes all properties of an object type optional.

/**
 * Makes all properties of an object type optional
 * @param schema - Object schema to make partial
 * @returns TPartial schema
 */
function Partial<T extends TSchema>(schema: T, options?: SchemaOptions): TPartial<T>;

Usage Examples:

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

const User = Type.Object({
  name: Type.String(),
  age: Type.Number(),
  email: Type.String()
});

// All properties become optional
const PartialUser = Type.Partial(User);
// Equivalent to: { name?: string, age?: number, email?: string }

Required Type

Makes all properties of an object type required.

/**
 * Makes all properties of an object type required
 * @param schema - Object schema to make required
 * @returns TRequired schema
 */
function Required<T extends TSchema>(schema: T, options?: SchemaOptions): TRequired<T>;

Pick Type

Picks specific properties from an object type.

/**
 * Picks specific properties from an object type
 * @param schema - Object schema to pick from
 * @param keys - Union of literal keys to pick
 * @returns TPick schema
 */
function Pick<T extends TObject, K extends TUnion<TLiteral[]>>(schema: T, keys: K, options?: SchemaOptions): TPick<T, K>;

Usage Examples:

const User = Type.Object({
  id: Type.String(),
  name: Type.String(),
  email: Type.String(),
  password: Type.String(),
  createdAt: Type.Date()
});

// Pick only specific properties
const PublicUser = Type.Pick(User, Type.Union([
  Type.Literal('id'),
  Type.Literal('name'),
  Type.Literal('email')
]));
// Result: { id: string, name: string, email: string }

Omit Type

Omits specific properties from an object type.

/**
 * Omits specific properties from an object type
 * @param schema - Object schema to omit from
 * @param keys - Union of literal keys to omit
 * @returns TOmit schema
 */
function Omit<T extends TObject, K extends TUnion<TLiteral[]>>(schema: T, keys: K, options?: SchemaOptions): TOmit<T, K>;

Usage Examples:

// Omit sensitive properties
const SafeUser = Type.Omit(User, Type.Union([
  Type.Literal('password'),
  Type.Literal('createdAt')
]);
// Result: { id: string, name: string, email: string }

Readonly Types

Makes properties readonly or both readonly and optional.

/**
 * Makes properties readonly
 * @param schema - Schema to make readonly
 * @returns TReadonly schema
 */
function Readonly<T extends TSchema>(schema: T): TReadonly<T>;

/**
 * Makes properties both readonly and optional
 * @param schema - Schema to make readonly and optional
 * @returns TReadonlyOptional schema
 */
function ReadonlyOptional<T extends TSchema>(schema: T): TReadonlyOptional<T>;

Record Type

Creates record types with key/value schemas.

/**
 * Creates record types with key/value schemas
 * @param key - Schema for record keys
 * @param value - Schema for record values
 * @returns TRecord schema
 */
function Record<K extends TSchema, V extends TSchema>(key: K, value: V): TRecord<K, V>;

Usage Examples:

// String keys, number values
const StringToNumber = Type.Record(Type.String(), Type.Number());
// Equivalent to: Record<string, number>

// Literal keys, object values
const UserRoles = Type.Record(
  Type.Union([Type.Literal('admin'), Type.Literal('user'), Type.Literal('guest')]),
  Type.Object({
    permissions: Type.Array(Type.String()),
    level: Type.Number()
  })
);

Intersect Type

Creates an intersection of multiple types (AND logic).

/**
 * Creates an intersection of multiple types (AND logic)
 * @param schemas - Schemas to intersect
 * @returns TIntersect schema
 */
function Intersect<T extends TSchema[]>(...schemas: [...T]): TIntersect<T>;

Usage Examples:

const Person = Type.Object({
  name: Type.String(),
  age: Type.Number()
});

const Employee = Type.Object({
  employeeId: Type.String(),
  department: Type.String()
});

// Intersection combines both types
const PersonEmployee = Type.Intersect([Person, Employee]);
// Result: { name: string, age: number, employeeId: string, department: string }

Conditional Types

Extends Type

Creates conditional types with ternary logic.

/**
 * Creates conditional types with ternary logic
 * @param left - Type to check
 * @param right - Type to check against
 * @param trueType - Type when condition is true
 * @param falseType - Type when condition is false
 * @returns TExtends schema
 */
function Extends<L extends TSchema, R extends TSchema, T extends TSchema, F extends TSchema>(
  left: L, 
  right: R, 
  trueType: T, 
  falseType: F
): TExtends<L, R, T, F>;

Exclude and Extract Types

Utility types for union manipulation.

/**
 * Excludes types from union
 * @param from - Union to exclude from
 * @param exclude - Types to exclude
 * @returns TExclude schema
 */
function Exclude<T extends TSchema, U extends TSchema>(from: T, exclude: U): TExclude<T, U>;

/**
 * Extracts matching types from union
 * @param from - Union to extract from
 * @param extract - Types to extract
 * @returns TExtract schema
 */
function Extract<T extends TSchema, U extends TSchema>(from: T, extract: U): TExtract<T, U>;

KeyOf Type

Creates union of object property keys.

/**
 * Creates union of object property keys
 * @param schema - Object schema to extract keys from
 * @returns TKeyOf schema
 */
function KeyOf<T extends TObject>(schema: T): TKeyOf<T>;

Usage Examples:

const User = Type.Object({
  id: Type.String(),
  name: Type.String(),
  email: Type.String()
});

const UserKeys = Type.KeyOf(User);
// Result: "id" | "name" | "email"

Template Literal Types

Creates template literal string types for pattern matching.

/**
 * Creates template literal string types
 * @param kinds - Array of string literals and type patterns
 * @returns TTemplateLiteral schema
 */
function TemplateLiteral<T extends TTemplateLiteralKind[]>(kinds: [...T]): TTemplateLiteral<T>;

type TTemplateLiteralKind = TLiteral<string> | TString | TNumber | TBoolean;

Usage Examples:

// Simple template literal
const Greeting = Type.TemplateLiteral([
  Type.Literal('Hello '),
  Type.String()
]);
// Matches: "Hello world", "Hello Alice", etc.

// Complex template literal
const EventName = Type.TemplateLiteral([
  Type.Literal('on'),
  Type.Union([Type.Literal('Click'), Type.Literal('Hover'), Type.Literal('Focus')]),
  Type.String()
]);
// Matches: "onClickButton", "onHoverMenu", "onFocusInput", etc.

Mapped Types

Creates mapped object types with transformations.

/**
 * Creates mapped object types with transformations
 * @param key - Union of keys to map over
 * @param schema - Schema to apply to each key
 * @returns TMapped schema
 */
function Mapped<K extends TUnion<TLiteral[]>, T extends TSchema>(key: K, schema: T): TMapped<K, T>;

Index Types

Creates indexed access types.

/**
 * Creates indexed access types
 * @param schema - Object schema to index into
 * @param key - Key to access
 * @returns TIndex schema
 */
function Index<T extends TObject, K extends TSchema>(schema: T, key: K): TIndex<T, K>;

Intrinsic String Functions

String transformation utilities for literal types.

/**
 * Capitalizes string literal types
 * @param schema - String literal schema
 * @returns TCapitalize schema
 */
function Capitalize<T extends TString>(schema: T): TCapitalize<T>;

/**
 * Lowercases string literal types
 * @param schema - String literal schema
 * @returns TLowercase schema
 */
function Lowercase<T extends TString>(schema: T): TLowercase<T>;

/**
 * Uppercases string literal types
 * @param schema - String literal schema
 * @returns TUppercase schema
 */
function Uppercase<T extends TString>(schema: T): TUppercase<T>;

/**
 * Uncapitalizes string literal types
 * @param schema - String literal schema
 * @returns TUncapitalize schema
 */
function Uncapitalize<T extends TString>(schema: T): TUncapitalize<T>;

Tuple Utilities

Utilities for working with tuple types.

/**
 * Extracts rest elements from Tuple/Intersect/Union
 * @param schema - Schema to extract from
 * @returns TRest schema
 */
function Rest<T extends TSchema>(schema: T): TRest<T>;

Not Type

Creates negation types (NOT logic).

/**
 * Creates negation types (NOT logic)
 * @param schema - Schema to negate
 * @returns TNot schema
 */
function Not<T extends TSchema>(schema: T): TNot<T>;

Usage Examples:

// Not a string (anything except string)
const NotString = Type.Not(Type.String());

// Not a specific value
const NotZero = Type.Not(Type.Literal(0));

Type Interfaces

interface TPartial<T extends TSchema> extends TSchema {
  [Kind]: 'Partial';
  type: 'object';
  properties: { [K in keyof T['properties']]: TOptional<T['properties'][K]> };
}

interface TRequired<T extends TSchema> extends TSchema {
  [Kind]: 'Required';
  type: 'object';
  properties: T['properties'];
  required: (keyof T['properties'])[];
}

interface TPick<T extends TObject, K extends TUnion<TLiteral[]>> extends TSchema {
  [Kind]: 'Pick';
  type: 'object';
  properties: Pick<T['properties'], K>;
}

interface TOmit<T extends TObject, K extends TUnion<TLiteral[]>> extends TSchema {
  [Kind]: 'Omit';
  type: 'object';
  properties: Omit<T['properties'], K>;
}

interface TIntersect<T extends TSchema[]> extends TSchema {
  allOf: T;
}

interface TRecord<K extends TSchema, V extends TSchema> extends TSchema {
  type: 'object';
  patternProperties: {
    [pattern: string]: V;
  };
}

interface TKeyOf<T extends TObject> extends TSchema {
  anyOf: TLiteral<keyof T['properties']>[];
}

interface TTemplateLiteral<T extends TTemplateLiteralKind[]> extends TSchema {
  type: 'string';
  pattern: string;
}

interface TNot<T extends TSchema> extends TSchema {
  not: T;
}

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