Code Reference

Generic Types

_TypeScript · Reference cheat sheet_

Generic Types

TypeScript · Reference cheat sheet


📖 Overview

Generics parameterize types and functions over other types (<T>), preserving relationships between inputs and outputs. Constraints (extends) limit what T may be; defaults provide fallbacks.

🧩 Core concepts

  • Type parametersfunction identity<T>(value: T): T.
  • ConstraintsT extends \{ id: string \} requires a minimum shape.
  • Defaultstype Box<T = string> = \{ value: T \}.
  • Generic interfaces / classes / type aliases — same idea across constructs.
  • Inference — usually from arguments; annotate when inference fails or is too wide.
  • keyof + indexed accessT[K] pairs well with constrained keys.

💡 Examples

function identity<T>(value: T): T {
  return value;
}
const n = identity(42); // number

function pluck<T, K extends keyof T>(obj: T, key: K): T[K] {
  return obj[key];
}
pluck({ name: "Ada", age: 36 }, "name"); // string

interface ApiResponse<T> {
  data: T;
  error?: string;
}

type Paginated<T> = {
  items: T[];
  total: number;
};

class Repo<T extends { id: string }> {
  constructor(private items: T[] = []) {}
  find(id: string): T | undefined {
    return this.items.find((i) => i.id === id);
  }
}

// Default type arg
type Result<T = unknown> = { ok: true; value: T } | { ok: false };

⚠️ Pitfalls

  • Unconstrained T inside a function is treated like unknown for property access.
  • Over-constraining (T extends any) adds noise without safety.
  • Generic rest/tuple inference can surprise — add explicit type args when needed.
  • Don’t use generics when a concrete union or overload is clearer.

On this page