Cheatsheets
TypeScript
Types, interfaces, generics, utility types, and narrowing.
Practice thisVisualize
TypeScript — type narrowing
Control-flow guards (typeof, ===, in) shrink a union to one specific type per branch.
Guards (typeof, ===, in, instanceof) narrow the union to one type per branch. Press play.
Garbage collection — mark & sweep (3D)
Liveness = reachability from the roots; anything the GC can't reach is swept (not reference-counting).
31 entries
Types7
string | number | booleanPrimitives & unions
string[] Array<string>Arrays
[string, number]Tuple (fixed positions)
"a" | "b"String literal union
any / unknown / neverEscape hatch / safe-unknown / impossible — prefer unknown over any.
{ id: number; name?: string }Object type (`?` = optional)
const x = [1, 2] as constas const — freeze to the narrowest literal, readonly type.
Interfaces & aliases6
interface User { id: number }Interface (extendable)
type ID = string | numberType alias (unions, etc.)
interface A extends B {}Inheritance
type C = A & BIntersection
readonly id: numberImmutable property
[key: string]: numberIndex signature
Functions & generics5
(a: number, b?: number) => numberFunction type (optional param)
function first<T>(arr: T[]): T | undefinedGenerics — keep the relationship between input and output types.
<T extends { id: number }>Generic constraint
function isFoo(x: unknown): x is FooType predicate — a function that narrows a type for the caller.
function f(): asserts x is FooAssertion signature
Utility types6
Partial<T> / Required<T>All optional / all required
Pick<T, K> / Omit<T, K>Build a new type by selecting / excluding keys.
Record<K, V>Map of K → V
Readonly<T>Immutable version
ReturnType<F> / Parameters<F>Infer a function's return / params
Awaited<P>Unwrap a Promise type
Narrowing & misc7
switch (s.kind) { ... }Discriminated union — narrow by a shared literal tag.
typeof x === "string"Typeof narrowing
x instanceof FooClass narrowing
keyof TUnion of a type's keys
obj satisfies ConfigCheck a value matches a type without widening it (4.9+).
enum Color { Red, Green }Enum
import type { Foo } from "./x"Type-only import