TypeScript
TypeScript utility snippets and helper functions
Debounce Function
Generic debounce function with immediate execution option
interface DebounceOptions {
immediate?: boolean;
maxWait?: number;
...Type Safe Guards
Comprehensive runtime type checking utilities
// Basic type guards
const isString = (value: unknown): value is string => {
return typeof value === 'string';
...Deep Partial Utility
Recursive Partial type for nested objects
// Basic DeepPartial type
type DeepPartial<T> = T extends object
? {
...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;
...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;
...Result Type Pattern
Type-safe error handling with Result type
// Base Result type
type Result<T, E = Error> = Success<T> | Failure<E>;
...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>;
...Observable Pattern
Simple Observable implementation with operators
type Observer<T> = {
next: (value: T) => void;
error?: (error: any) => void;
...Retry Mechanism with Exponential Backoff
Configurable retry utility for async operations with backoff strategies
interface RetryOptions<T> {
maxAttempts?: number;
delay?: number;
...Finite State Machine
Type-safe finite state machine implementation
type Transition<State extends string, Event extends string> = [State, Event, State];
interface StateMachineConfig<
...Functional Programming Utilities
Collection of functional programming utilities with TypeScript
// Currying
function curry<T extends any[], R>(
fn: (...args: T) => R
...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;
...Type Safe Configuration Manager
Hierarchical configuration management with validation and environment support
interface ConfigSchema {
[key: string]: {
type: 'string' | 'number' | 'boolean' | 'object' | 'array';
...Middleware Pipeline Pattern
Composable middleware pipeline for request/response processing
type Middleware<T> = (
context: T,
next: () => Promise<void> | void
...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;
...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
...