Code Explainers

Typescript code explainers

typescript
import {
  Injectable,
  NestInterceptor,
  ExecutionContext,

How a NestJS logging interceptor works

interceptors rxjs logging
Intermediate 5 steps
typescript
// A module-scoped Singleton: the single instance lives in this module's
// closure and is never exposed directly.
 
interface AppConfig {

A module-scoped Singleton in TypeScript

singleton generics encapsulation
Intermediate 7 steps
typescript
interface RetryOptions {
  retries?: number;
  baseDelayMs?: number;
  maxDelayMs?: number;

Retrying async tasks with exponential backoff

retry exponential-backoff async
Intermediate 8 steps
typescript
type NestedArray<T> = Array<T | NestedArray<T>>;
 
function flatten<T>(input: NestedArray<T>): T[] {
  const result: T[] = [];

Recursively flattening nested arrays in TypeScript

recursion recursive-types generics
Intermediate 7 steps
typescript
class LRUCache<K, V> {
  private readonly capacity: number;
  private readonly map: Map<K, V>;
 

Building an LRU cache on a JS Map

caching data-structures generics
Intermediate 8 steps
typescript
type Split<S extends string, D extends string> =
  S extends `${infer Head}${D}${infer Tail}`
    ? [Head, ...Split<Tail, D>]
    : [S];

Type-level string parsing in TypeScript

template literal types conditional types recursive types
Advanced 8 steps