CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-ts-toolbelt

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

Pending
Overview
Eval results
Files

any.mddocs/

Any Module

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

Capabilities

Type Casting

Cast types with fallback behavior when casting fails.

/**
 * Cast type A1 to A2, falling back to A2 if cast fails
 * @param A1 - Type to cast from
 * @param A2 - Type to cast to
 * @returns A1 if it extends A2, otherwise A2
 */
type Cast<A1, A2> = A1 extends A2 ? A1 : A2;

Usage Examples:

import { A } from "ts-toolbelt";

type Result1 = A.Cast<"hello", string>; // "hello"
type Result2 = A.Cast<42, string>; // string
type Result3 = A.Cast<{ a: 1 }, { a: number }>; // { a: 1 }

Type Equality and Relationships

Check strict type equality and extends relationships between types.

/**
 * Check if two types are strictly equal
 * @param A1 - First type to compare
 * @param A2 - Second type to compare  
 * @returns 1 if types are equal, 0 otherwise
 */
type Equals<A1, A2> = (<A>() => A extends A1 ? 1 : 0) extends (<A>() => A extends A2 ? 1 : 0) ? 1 : 0;

/**
 * Check if type A extends type B
 * @param A - Type to check
 * @param B - Type to check against
 * @returns 1 if A extends B, 0 otherwise
 */
type Extends<A, B> = A extends B ? 1 : 0;

/**
 * Check if type A contains type B
 * @param A - Container type
 * @param B - Type to look for
 * @returns 1 if A contains B, 0 otherwise
 */
type Contains<A, B> = B extends A ? 1 : 0;

Usage Examples:

import { A } from "ts-toolbelt";

type Equal1 = A.Equals<string, string>; // 1
type Equal2 = A.Equals<string, number>; // 0
type Equal3 = A.Equals<"hello", string>; // 0

type Extends1 = A.Extends<"hello", string>; // 1
type Extends2 = A.Extends<string, "hello">; // 0
type Extends3 = A.Extends<never, any>; // 1

type Contains1 = A.Contains<string | number, string>; // 1
type Contains2 = A.Contains<string, number>; // 0

Property Access

Access properties and extract keys from types.

/**
 * Describes index keys for any type
 * @returns Union of string, number, and symbol
 */
type Key = string | number | symbol;

/**
 * Access property at key K in type O
 * @param O - Object type to access
 * @param K - Key to access
 * @returns Property type at key K
 */
type At<O, K extends keyof O> = O[K];

/**
 * Extract all keys from type O
 * @param O - Type to extract keys from
 * @returns Union of all keys
 */
type Keys<O> = keyof O;

/**
 * Extract known keys (non-index signature) from type O
 * @param O - Type to extract known keys from
 * @returns Union of known keys
 */
type KnownKeys<O> = keyof O;

Usage Examples:

import { A } from "ts-toolbelt";

type User = { name: string; age: number; email: string };

// Key type for valid property keys
type ValidKey = A.Key; // string | number | symbol
type StringKey: A.Key = "name"; // Valid
type NumberKey: A.Key = 0; // Valid
type SymbolKey: A.Key = Symbol("key"); // Valid

type Name = A.At<User, "name">; // string
type UserKeys = A.Keys<User>; // "name" | "age" | "email"
type KnownUserKeys = A.KnownKeys<User>; // "name" | "age" | "email"

// With index signatures
type IndexedType = { [key: string]: any; knownProp: number };
type IndexedKeys = A.Keys<IndexedType>; // string | "knownProp"
type KnownKeys = A.KnownKeys<IndexedType>; // "knownProp"

Type Computation

Control TypeScript's type computation and evaluation.

/**
 * Force TypeScript to evaluate intersections and compute final type
 * @param O - Object type to compute
 * @param depth - Computation depth: 'flat' or 'deep'
 * @returns Computed type with forced evaluation
 */
type Compute<O extends Record<string, any>, depth extends 'flat' | 'deep' = 'flat'> = 
  depth extends 'flat' ? { [K in keyof O]: O[K] } & {} : ComputeDeep<O>;

Usage Examples:

import { A } from "ts-toolbelt";

type Base = { a: number };
type Extended = Base & { b: string };

type Computed = A.Compute<Extended>; // { a: number; b: string }

// Deep computation for nested intersections
type NestedIntersection = {
  user: { name: string } & { age: number };
  data: { id: string } & { value: number };
};

type DeepComputed = A.Compute<NestedIntersection, 'deep'>; 
// {
//   user: { name: string; age: number };
//   data: { id: string; value: number };
// }

Promise Operations

Handle promise types and async operations.

/**
 * Await/unwrap promise types
 * @param P - Promise type to await
 * @returns Resolved value type of promise
 */
type Await<P> = P extends Promise<infer T> ? T : P;

/**
 * Promise-related type utilities
 */
type Promise<T> = globalThis.Promise<T>;

Usage Examples:

import { A } from "ts-toolbelt";

type AsyncString = Promise<string>;
type SyncString = A.Await<AsyncString>; // string

type AsyncUser = Promise<{ name: string; age: number }>;
type User = A.Await<AsyncUser>; // { name: string; age: number }

// Works with non-promises too
type AlreadySync = A.Await<number>; // number

Type Utilities

Additional type checking and manipulation utilities.

/**
 * Generic type checking utility
 * @param A - Type to check
 * @param C - Condition/constraint
 * @returns Type checking result
 */
type Is<A, C> = A extends C ? 1 : 0;

/**
 * Error handling for types - try type operations safely
 * @param A - Type to try operation on
 * @param Catch - Fallback type if operation fails
 * @returns Result of operation or fallback
 */
type Try<A, Catch = never> = A extends never ? Catch : A;

/**
 * Placeholder type for currying operations
 */
type x = any;

/**
 * Basic type operations
 */
type Type = any;

Usage Examples:

import { A } from "ts-toolbelt";

// Type checking
type IsString = A.Is<"hello", string>; // 1
type IsNumber = A.Is<"hello", number>; // 0

// Safe type operations
type SafeOperation = A.Try<string, "fallback">; // string
type FailedOperation = A.Try<never, "fallback">; // "fallback"

// Currying placeholder
type CurriedFn<T> = T extends A.x ? number : T;
type Result1 = CurriedFn<A.x>; // number
type Result2 = CurriedFn<string>; // string

Types

// Core types used by Any module
type Key = string | number | symbol;

Install with Tessl CLI

npx tessl i tessl/npm-ts-toolbelt

docs

any.md

boolean.md

class.md

community.md

function.md

index.md

iteration.md

list.md

misc.md

number.md

object.md

string.md

testing.md

union.md

tile.json