or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

any.mdboolean.mdclass.mdcommunity.mdfunction.mdindex.mditeration.mdlist.mdmisc.mdnumber.mdobject.mdstring.mdtesting.mdunion.md
tile.json

tessl/npm-ts-toolbelt

TypeScript's largest utility library providing 200+ type utilities for advanced type manipulation.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/ts-toolbelt@9.6.x

To install, run

npx @tessl/cli install tessl/npm-ts-toolbelt@9.6.0

index.mddocs/

ts-toolbelt

TypeScript's largest utility library providing 200+ type utilities for advanced type manipulation. It offers sophisticated type-level programming capabilities with mapped types, conditional types, and recursive types for performing complex transformations on objects, unions, functions, strings, numbers, and other TypeScript constructs.

Package Information

  • Package Name: ts-toolbelt
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install ts-toolbelt

Core Imports

import { A, Any, B, Boolean, C, Class, F, Function, L, List, N, Number, O, Object, S, String, U, Union } from "ts-toolbelt";

For CommonJS:

const { A, Any, B, Boolean, C, Class, F, Function, L, List, N, Number, O, Object, S, String, U, Union } = require("ts-toolbelt");

Basic Usage

import { O, F, L, S } from "ts-toolbelt";

// Object manipulation
type User = { name: string; age: number; email: string };
type PartialUser = O.Partial<User>; // Make all properties optional
type PickedUser = O.Pick<User, "name" | "email">; // Pick specific properties

// Function composition and currying  
type Add = (a: number) => (b: number) => number;
type CurriedAdd = F.Curry<Add>; // Enable currying with placeholders

// List operations
type Numbers = [1, 2, 3, 4, 5];
type Reversed = L.Reverse<Numbers>; // [5, 4, 3, 2, 1]
type FirstTwo = L.Take<Numbers, 2>; // [1, 2]

// String manipulation
type Text = "hello-world";
type Parts = S.Split<Text, "-">; // ["hello", "world"]
type Length = S.Length<Text>; // 11

Architecture

ts-toolbelt is built around several key principles:

  • Modular Design: Each domain (Any, Boolean, Class, etc.) has its own module with dedicated _api.ts exports
  • Dual Exports: Both shorthand (single letter) and full names for each module (e.g., A/Any, O/Object)
  • Performance Optimization: Non-distributed versions available with _ prefix for internal use
  • Type-Level Programming: Comprehensive arithmetic, boolean logic, and complex transformations at the type level
  • Distribution Handling: Careful management of type distribution across unions for correctness and performance

Capabilities

Any Module

Generic type utilities for fundamental type operations including casting, equality checking, and property access.

// Core casting and type checking
type Cast<A1, A2> = A1 extends A2 ? A1 : A2;
type Equals<A1, A2> = (<A>() => A extends A1 ? 1 : 0) extends (<A>() => A extends A2 ? 1 : 0) ? 1 : 0;
type Extends<A, B> = A extends B ? 1 : 0;

// Property access and key operations
type At<O, K extends keyof O> = O[K];
type Keys<O> = keyof O;
type KnownKeys<O> = keyof O;

// Type computation control
type Compute<O extends Record<string, any>, depth extends 'flat' | 'deep' = 'flat'> = 
  depth extends 'flat' ? { [K in keyof O]: O[K] } & {} : ComputeDeep<O>;

Any Module

Boolean Module

Type-level boolean logic operations using lookup tables for AND, OR, NOT, and XOR operations.

type Boolean = 0 | 1;
type Not<B extends Boolean> = B extends 0 ? 1 : 0;
type And<B1 extends Boolean, B2 extends Boolean> = B1 extends 1 ? B2 : 0;
type Or<B1 extends Boolean, B2 extends Boolean> = B1 extends 1 ? 1 : B2;
type Xor<B1 extends Boolean, B2 extends Boolean> = B1 extends B2 ? 0 : 1;

Boolean Module

Class Module

Class-related utilities for extracting instance types and constructor parameters.

type Class<A = any, P extends readonly any[] = any> = new (...args: P) => A;
type Instance<C extends Class> = C extends Class<infer I> ? I : never;
type Parameters<C extends Class> = C extends Class<any, infer P> ? P : never;

Class Module

Function Module

Advanced function manipulation including composition, currying, and piping with placeholder support.

type Function<P extends readonly any[] = any, R = any> = (...args: P) => R;
type Parameters<F extends Function> = F extends Function<infer P> ? P : never;
type Return<F extends Function> = F extends Function<any, infer R> ? R : never;

// Advanced currying with placeholder support
type Curry<F extends Function> = F extends Function<infer P, infer R> 
  ? P extends readonly [] ? () => R 
  : CurryImpl<P, R> : never;

// Function composition and piping
type Compose<Fns extends readonly Function[]> = ComposeImpl<Fns>;
type Pipe<F extends Function, Fns extends readonly Function[]> = PipeImpl<F, Fns>;

Function Module

List Module

Comprehensive array and tuple manipulation - the largest module with 73+ operations.

type List<A = any> = readonly A[];

// Core manipulation
type Append<L extends List, A> = [...L, A];
type Prepend<L extends List, A> = [A, ...L];
type Concat<L1 extends List, L2 extends List> = [...L1, ...L2];
type Reverse<L extends List> = ReverseImpl<L>;

// Access operations  
type At<L extends List, K extends number> = L[K];
type Head<L extends List> = L extends readonly [infer H, ...any[]] ? H : never;
type Tail<L extends List> = L extends readonly [any, ...infer T] ? T : never;
type Last<L extends List> = L extends readonly [...any[], infer L] ? L : never;

// Transformation operations
type Filter<L extends List, M> = FilterImpl<L, M>;
type Map<L extends List, F> = MapImpl<L, F>;
type Zip<L1 extends List, L2 extends List> = ZipImpl<L1, L2>;

List Module

Number Module

Type-level arithmetic operations using an internal iteration system supporting operations from -100 to 100.

type Add<N1 extends number, N2 extends number> = AddImpl<N1, N2>;
type Sub<N1 extends number, N2 extends number> = SubImpl<N1, N2>;
type Absolute<N extends number> = AbsoluteImpl<N>;
type Negate<N extends number> = NegateImpl<N>;

// Comparison operations
type Greater<N1 extends number, N2 extends number> = GreaterImpl<N1, N2>;
type GreaterEq<N1 extends number, N2 extends number> = GreaterEqImpl<N1, N2>;
type Lower<N1 extends number, N2 extends number> = LowerImpl<N1, N2>;
type LowerEq<N1 extends number, N2 extends number> = LowerEqImpl<N1, N2>;

// Utility checks
type IsZero<N extends number> = N extends 0 ? 1 : 0;
type IsPositive<N extends number> = IsPositiveImpl<N>;
type IsNegative<N extends number> = IsNegativeImpl<N>;

Number Module

Object Module

Extensive object manipulation utilities with deep property operations and path-based access.

type Object = Record<string | number | symbol, any>;

// Property access
type At<O extends Object, K extends keyof O> = O[K];
type Keys<O extends Object> = keyof O;
type Values<O extends Object> = O[keyof O];
type Paths<O extends Object> = PathsImpl<O>;

// Core transformations
type Pick<O extends Object, K extends keyof O> = { [P in K]: O[P] } & {};
type Omit<O extends Object, K extends keyof O> = Pick<O, Exclude<keyof O, K>>;
type Merge<O1 extends Object, O2 extends Object, depth extends 'flat' | 'deep' = 'flat'> = 
  depth extends 'flat' ? O1 & O2 : MergeDeep<O1, O2>;

// Property modifications
type Partial<O extends Object> = { [K in keyof O]?: O[K] };
type Required<O extends Object> = { [K in keyof O]-?: O[K] };
type Readonly<O extends Object> = { readonly [K in keyof O]: O[K] };
type Writable<O extends Object> = { -readonly [K in keyof O]: O[K] };

Object Module

String Module

Template literal-based string manipulation utilities.

type At<S extends string, N extends number> = AtImpl<S, N>;
type Length<S extends string> = LengthImpl<S>;
type Split<S extends string, D extends string> = S extends `${infer B}${D}${infer A}` 
  ? [B, ...Split<A, D>] : [S];
type Join<L extends readonly string[], D extends string> = JoinImpl<L, D>;
type Replace<S extends string, From extends string, To extends string> = 
  S extends `${infer B}${From}${infer A}` ? `${B}${To}${Replace<A, From, To>}` : S;

String Module

Union Module

Union type operations including set operations, filtering, and transformations.

// Set operations
type Exclude<U, E> = U extends E ? never : U;
type Extract<U, E> = U extends E ? U : never;
type Intersect<U1, U2> = Extract<U1, U2>;
type Diff<U1, U2> = Exclude<U1, U2>;

// Transformations
type Strict<U> = StrictImpl<U>;
type Merge<U> = MergeImpl<U>;
type IntersectOf<U> = IntersectOfImpl<U>;
type ListOf<U> = ListOfImpl<U>;

// Utilities
type Has<U, A> = A extends U ? 1 : 0;
type Last<U> = LastImpl<U>;
type Pop<U> = PopImpl<U>;

Union Module

Testing Module

Built-in type testing utilities for validating type transformations.

type Pass = 1;
type Fail = 0;

declare function check<Type, Expect, Outcome extends Boolean>(
  debug?: Equals<Equals<Type, Expect>, Outcome>
): Equals<Equals<Type, Expect>, Outcome>;

declare function checks(checks: 1[]): void;

Testing Module

Community Module

Community-contributed utilities for deep inclusion checking and literal type validation.

type IncludesDeep<T, U> = IncludesDeepImpl<T, U>;
type IsLiteral<T> = IsLiteralImpl<T>;

Community Module

Misc Module

Miscellaneous utilities including built-in types, primitives, and JSON handling.

type BuiltIn = Function | Error | Date | { readonly [Symbol.toStringTag]: string } | RegExp | Generator;
type Primitive = string | number | boolean | bigint | symbol | undefined | null;

namespace JSON {
  type Primitive = string | number | boolean | null;
  type Array = Value[];
  type Object = { [key: string]: Value };
  type Value = Primitive | Object | Array;
}

Misc Module

Iteration Module

Internal iteration system providing type-level arithmetic infrastructure.

type Iteration = [number, '-' | '0' | '+', IterationKey, IterationKey, IterationKey];
type IterationOf<N extends number> = IterationOfImpl<N>;
type Next<I extends Iteration> = NextImpl<I>;
type Prev<I extends Iteration> = PrevImpl<I>;
type Pos<I extends Iteration> = PosImpl<I>;
type Key = string;

Iteration Module

Types

// Core utility types used across modules
type Key = string | number | symbol;
type Boolean = 0 | 1;
type List<A = any> = readonly A[];
type Object = Record<Key, any>;
type Function<P extends readonly any[] = any, R = any> = (...args: P) => R;
type BuiltIn = Function | Error | Date | { readonly [Symbol.toStringTag]: string } | RegExp | Generator;
type Primitive = string | number | boolean | bigint | symbol | undefined | null;