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

function.mddocs/

Function Module

Advanced function manipulation including composition, currying, piping, and parameter extraction with sophisticated type-level programming support.

Capabilities

Function Type Basics

Core function type utilities for parameter and return type extraction.

/**
 * Base function type
 * @param P - Parameter types as tuple
 * @param R - Return type
 */
type Function<P extends readonly any[] = any, R = any> = (...args: P) => R;

/**
 * Extract parameter types from function
 * @param F - Function to extract parameters from
 * @returns Tuple of parameter types
 */
type Parameters<F extends Function> = F extends Function<infer P> ? P : never;

/**
 * Extract return type from function
 * @param F - Function to extract return type from
 * @returns Return type of function
 */
type Return<F extends Function> = F extends Function<any, infer R> ? R : never;

/**
 * Get length of function parameters
 * @param F - Function to get parameter length from
 * @returns Number of parameters
 */
type Length<F extends Function> = Parameters<F>['length'];

Usage Examples:

import { F } from "ts-toolbelt";

type MyFunction = (a: string, b: number, c?: boolean) => void;

type Params = F.Parameters<MyFunction>; // [string, number, boolean?]
type ReturnType = F.Return<MyFunction>; // void
type ParamCount = F.Length<MyFunction>; // 3

// With arrow functions
type ArrowFn = (x: number) => string;
type ArrowParams = F.Parameters<ArrowFn>; // [number]
type ArrowReturn = F.Return<ArrowFn>; // string

Function Currying

Advanced currying with placeholder support for partial application.

/**
 * Transform function into curried version with placeholder support
 * @param F - Function to curry
 * @returns Curried function that accepts partial arguments
 */
type Curry<F extends Function> = F extends Function<infer P, infer R> 
  ? P extends readonly [] 
    ? () => R 
    : CurryImpl<P, R> 
  : never;

/**
 * Reverse curry operation - flatten curried function back to regular function
 * @param F - Curried function to uncurry
 * @returns Regular function with all parameters
 */
type UnCurry<F extends Function> = UnCurryImpl<F>;

/**
 * Placeholder type for currying - represents unfilled argument position
 */
type _ = any;

Usage Examples:

import { F } from "ts-toolbelt";

// Basic currying
type Add = (a: number, b: number, c: number) => number;
type CurriedAdd = F.Curry<Add>; 
// (a: number) => (b: number) => (c: number) => number
// OR (a: number, b: number) => (c: number) => number  
// OR (a: number, b: number, c: number) => number

// Using placeholders
type CurriedWithPlaceholder = F.Curry<Add>;
// Can be called as: fn(_, 2, _) then fn(1, 3) to get result

// Uncurrying
type RegularAdd = F.UnCurry<CurriedAdd>; // (a: number, b: number, c: number) => number

// Complex function currying  
type ComplexFn = (a: string, b: number, c: boolean, d: object) => string[];
type CurriedComplex = F.Curry<ComplexFn>;
// Supports partial application at any level with placeholders

Function Composition

Compose functions together for pipeline-style programming.

/**
 * Compose functions right-to-left (mathematical composition)
 * @param Fns - Array of functions to compose
 * @returns Single composed function
 */
type Compose<Fns extends readonly Function[]> = ComposeImpl<Fns>;

/**
 * Pipe functions left-to-right (pipeline composition)
 * @param F - Initial function
 * @param Fns - Array of functions to pipe through
 * @returns Single piped function
 */
type Pipe<F extends Function, Fns extends readonly Function[]> = PipeImpl<F, Fns>;

Usage Examples:

import { F } from "ts-toolbelt";

// Function composition (right-to-left)
type StringToNumber = (s: string) => number;
type NumberToBoolean = (n: number) => boolean;
type BooleanToString = (b: boolean) => string;

type Composed = F.Compose<[BooleanToString, NumberToBoolean, StringToNumber]>;
// (s: string) => string
// Applies: string -> number -> boolean -> string

// Function piping (left-to-right)  
type Piped = F.Pipe<StringToNumber, [NumberToBoolean, BooleanToString]>;
// (s: string) => string
// Applies: string -> number -> boolean -> string

// Complex pipeline
type ParseInt = (s: string) => number;
type IsEven = (n: number) => boolean;
type BoolToString = (b: boolean) => string;
type ToUpperCase = (s: string) => string;

type Pipeline = F.Pipe<ParseInt, [IsEven, BoolToString, ToUpperCase]>;
// (s: string) => string

Function Transformation

Transform and modify function types in various ways.

/**
 * Make function parameters narrower (more specific)
 * @param F - Function to narrow
 * @param N - Narrowing specification
 * @returns Function with narrowed parameters
 */
type Narrow<F extends Function, N> = NarrowImpl<F, N>;

/**
 * Make function exact - prevent excess properties
 * @param F - Function to make exact
 * @returns Exact function type
 */
type Exact<F extends Function> = ExactImpl<F>;

/**
 * Convert function to return promises
 * @param F - Function to promisify
 * @returns Function that returns Promise of original return type
 */
type Promisify<F extends Function> = F extends Function<infer P, infer R> 
  ? Function<P, Promise<R>>
  : never;

/**
 * Prevent type inference for function parameters
 * @param F - Function to prevent inference on
 * @returns Function with inference prevented
 */
type NoInfer<F extends Function> = NoInferImpl<F>;

Usage Examples:

import { F } from "ts-toolbelt";

// Promisify function
type SyncFunction = (x: number) => string;
type AsyncFunction = F.Promisify<SyncFunction>; // (x: number) => Promise<string>

// Narrow function parameters
type GenericFn = (obj: { a: any; b: any }) => void;
type NarrowedFn = F.Narrow<GenericFn, { a: string; b: number }>; 
// (obj: { a: string; b: number }) => void

// Exact function parameters
type LooseFn = (obj: { a: string }) => void;
type ExactFn = F.Exact<LooseFn>; // Prevents excess properties in obj

// Prevent inference
type InferenceFn<T> = (x: T) => T;
type NoInferenceFn<T> = F.NoInfer<InferenceFn<T>>; // T won't be inferred from usage

Path Operations

Generate and validate paths through function parameter structures.

/**
 * Automatically generate valid paths for function parameters
 * @param F - Function to generate paths for
 * @returns Valid path types for function parameters
 */
type AutoPath<F extends Function> = AutoPathImpl<F>;

/**
 * Validate that paths are valid for function parameters
 * @param F - Function to validate paths against
 * @param Path - Path to validate
 * @returns 1 if path is valid, 0 otherwise
 */
type ValidPath<F extends Function, Path extends string> = ValidPathImpl<F, Path>;

Usage Examples:

import { F } from "ts-toolbelt";

type ComplexFn = (user: { 
  profile: { 
    name: string; 
    details: { age: number; address: { city: string } } 
  }; 
  settings: { theme: string } 
}) => void;

type ValidPaths = F.AutoPath<ComplexFn>; 
// "0" | "0.profile" | "0.profile.name" | "0.profile.details" | 
// "0.profile.details.age" | "0.profile.details.address" | 
// "0.profile.details.address.city" | "0.settings" | "0.settings.theme"

type IsValidPath1 = F.ValidPath<ComplexFn, "0.profile.name">; // 1
type IsValidPath2 = F.ValidPath<ComplexFn, "0.invalid.path">; // 0

Types

// Core types used by Function module
type Function<P extends readonly any[] = any, R = any> = (...args: P) => R;
type _ = any; // Placeholder for currying

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