TypeScript

TypeScript utility snippets and helper functions

16Snippets
TYPESCRIPT

Debounce Function

Generic debounce function with immediate execution option

interface DebounceOptions { immediate?: boolean; maxWait?: number; ...
TYPESCRIPT

Type Safe Guards

Comprehensive runtime type checking utilities

// Basic type guards const isString = (value: unknown): value is string => { return typeof value === 'string'; ...
TYPESCRIPT

Deep Partial Utility

Recursive Partial type for nested objects

// Basic DeepPartial type type DeepPartial<T> = T extends object ? { ...
TYPESCRIPT

Type Safe Event Emitter

Generic event emitter with type-safe event handling

type EventMap = Record<string, any>; type EventKey<T extends EventMap> = string & keyof T; type EventCallback<T> = (data: T) => void; ...
TYPESCRIPT

Cache Manager with TTL

Generic cache manager with time-to-live and size limits

interface CacheOptions<K, V> { ttl?: number; // Time to live in milliseconds maxSize?: number; ...
TYPESCRIPT

Result Type Pattern

Type-safe error handling with Result type

// Base Result type type Result<T, E = Error> = Success<T> | Failure<E>; ...
TYPESCRIPT

Builder Pattern with Fluent API

Type-safe builder pattern for complex object creation

// Base Builder interface type Builder<T> = { [K in keyof T]-?: (value: T[K]) => Builder<T>; ...
TYPESCRIPT

Observable Pattern

Simple Observable implementation with operators

type Observer<T> = { next: (value: T) => void; error?: (error: any) => void; ...
TYPESCRIPT

Retry Mechanism with Exponential Backoff

Configurable retry utility for async operations with backoff strategies

interface RetryOptions<T> { maxAttempts?: number; delay?: number; ...
TYPESCRIPT

Finite State Machine

Type-safe finite state machine implementation

type Transition<State extends string, Event extends string> = [State, Event, State]; interface StateMachineConfig< ...
TYPESCRIPT

Functional Programming Utilities

Collection of functional programming utilities with TypeScript

// Currying function curry<T extends any[], R>( fn: (...args: T) => R ...
TYPESCRIPT

Lightweight Dependency Injection Container

Simple yet powerful DI container with lifecycle management and scopes

interface ServiceDescriptor<T = any> { token: string | symbol | Function; factory: (...args: any[]) => T; ...
TYPESCRIPT

Type Safe Configuration Manager

Hierarchical configuration management with validation and environment support

interface ConfigSchema { [key: string]: { type: 'string' | 'number' | 'boolean' | 'object' | 'array'; ...
TYPESCRIPT

Middleware Pipeline Pattern

Composable middleware pipeline for request/response processing

type Middleware<T> = ( context: T, next: () => Promise<void> | void ...
TYPESCRIPT

Immutable Data Builder

Builder pattern for creating immutable data structures with structural sharing

// Base immutable type type Immutable<T> = T extends Function ? T : T extends object ? { readonly [K in keyof T]: Immutable<T[K]> } : T; ...
TYPESCRIPT

Priority Scheduler Queue

Priority-based task scheduler with concurrency control and task dependency management

interface Task<T = any> { id: string; priority: number; // Higher number = higher priority ...