Typescript Examples

Page 2 of 3

typescriptSnippet

Next SSR Error Boundary Recovery

Contain server rendering failures in Next.js with route-level error boundaries, distinguishing retryable render errors from hard failures.

typescriptSnippet

Pino Multi-Transport Log Routing

Route Pino logs to multiple transports in parallel, sending errors to a dedicated sink while pretty console output stays development-only.

typescriptSnippet

Token Bucket Rate Limiter Async

Implement an async token bucket limiter with burst capacity, refill rate, per-key isolation, and wait semantics for queue-friendly callers.

typescriptSnippet

User-Defined Type Guards

Write predicate functions that narrow unknown values to concrete types, turning runtime checks into compile-time guarantees at the call site.

typescriptSnippet

Type-Safe Event Map Emitter

Bind event names to their payload types in one map so listeners and emitters are checked together and misspelled events fail to compile.

typescriptSnippet

Typed Result for Error Handling

Model success and failure as a discriminated Result type instead of throwing, forcing callers to handle the error branch explicitly at compile time.

typescriptSnippet

Vitest Module Mock Unit Test

Isolate a module under test with Vitest vi.mock hoisting, typed mock functions, and restore between tests so suites stay order-independent.

typescriptSnippet

Fixed-Size Circular Buffer

Store the most recent items in a fixed-capacity circular buffer with overwrite eviction, indexed access, iteration, and clearing.

typescriptSnippet

CSRF Double-Submit Token

Issue HMAC-bound CSRF tokens and verify that the header, cookie and session agree before accepting a state change.

typescriptSnippet

Dotenv File Parser

Parse .env files with comments, export prefixes, quoted values, escapes and multiline strings without mutating process.env.

typescriptSnippet

Glob Pattern Matcher

Compile glob patterns with star, globstar, question marks, character classes and braces into anchored regular expressions.