Code Explainers

Code explainers tagged #generics

typescript
type CsvColumn<T> = {
  header: string;
  value: (row: T) => string | number | boolean | null | undefined;
};

Building a type-safe CSV writer in TypeScript

generics serialization escaping
Intermediate 7 steps
typescript
import { useCallback, useEffect, useRef, useState } from "react";
 
interface Page<T> {
  items: T[];

A cursor-based infinite scroll hook in React

custom-hooks pagination intersection-observer
Intermediate 9 steps
go
package batch
 
import "fmt"
 

Splitting a slice into batches in Go

generics slices error-handling
Intermediate 6 steps
java
public final class DeepCopier {
 
    private static final ObjectMapper MAPPER = new ObjectMapper()
            .registerModule(new JavaTimeModule())

Deep-copying objects via Jackson serialization

serialization deep-copy generics
Intermediate 7 steps
rust
use std::any::{Any, TypeId};
use std::collections::HashMap;
 
pub trait Event: Any {

A type-safe event bus in Rust

type-erasure trait-objects downcasting
Advanced 8 steps
go
package deepcopy
 
import (
	"bytes"

Deep copying any value with gob in Go

deep-copy serialization generics
Intermediate 6 steps
typescript
function* map<T, U>(source: Iterable<T>, fn: (value: T, index: number) => U): Generator<U> {
  let index = 0;
  for (const value of source) {
    yield fn(value, index++);

Building a lazy iterator pipeline in TypeScript

generators lazy-evaluation iterators
Intermediate 7 steps
typescript
import { IsInt, IsOptional, Max, Min } from 'class-validator';
import { Type } from 'class-transformer';
 
export class PaginationQueryDto {

Type-safe pagination queries in NestJS

pagination validation dto
Intermediate 7 steps
typescript
import { useEffect, useRef, useState } from "react";
 
export function useClickOutside<T extends HTMLElement>() {
  const ref = useRef<T>(null);

A dismiss-on-outside-click hook in React

custom-hooks event-listeners cleanup
Intermediate 7 steps
typescript
type SortDirection = "asc" | "desc";
 
interface SortKey<T> {
  selector: (row: T) => string | number | Date | null | undefined;

Multi-column sorting in TypeScript

generics comparators sorting
Intermediate 6 steps
typescript
import { z, type ZodType } from "zod";
 
export class HttpError extends Error {
  constructor(

A type-safe fetch wrapper with Zod

generics runtime-validation error-handling
Intermediate 8 steps
typescript
type JsonValue = Record<string, unknown> | unknown[];
 
export function parseJsonStream<T = JsonValue>(
  stream: ReadableStream<Uint8Array>,

Streaming JSON parsing with a depth counter

streams parsing state-machine
Advanced 9 steps
java
public final class ListPartitioner {
 
    private ListPartitioner() {
    }

Splitting a list into fixed-size chunks in Java

generics partitioning streams
Intermediate 8 steps
typescript
type StorageSchema = Record<string, unknown>;
 
interface StorageOptions {
  namespace?: string;

A type-safe wrapper around localStorage

generics type-safety serialization
Intermediate 9 steps
typescript
type Fetcher<T> = (key: string) => Promise<T>;
 
export class RequestDeduplicator<T> {
  private inFlight = new Map<string, Promise<T>>();

Deduplicating in-flight requests in TypeScript

deduplication promises caching
Intermediate 7 steps
typescript
type EventMap = Record<string, unknown>;
 
type Handler<T> = (payload: T) => void;
 

A type-safe event bus in TypeScript

generics event-emitter type-safety
Advanced 7 steps
typescript
export class PriorityQueue<T> {
  private heap: Array<{ value: T; priority: number }> = [];
 
  get size(): number {

Building a binary-heap priority queue in TypeScript

binary-heap priority-queue generics
Intermediate 9 steps
java
@Service
public class OrderMetricsService {
 
    private final MeterRegistry registry;

Instrumenting orders with Micrometer in Spring

metrics micrometer observability
Intermediate 8 steps