All cheatsheets

Cheatsheets

TypeScript

Types, interfaces, generics, utility types, and narrowing.

Practice this

Visualize

TypeScript — type narrowing

Control-flow guards (typeof, ===, in) shrink a union to one specific type per branch.

x:stringnumbernull

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).

Loading 3D…

31 entries

Types7

string | number | boolean

Primitives & unions

string[] Array<string>

Arrays

[string, number]

Tuple (fixed positions)

"a" | "b"

String literal union

any / unknown / never

Escape hatch / safe-unknown / impossible — prefer unknown over any.

{ id: number; name?: string }

Object type (`?` = optional)

const x = [1, 2] as const

as const — freeze to the narrowest literal, readonly type.

Interfaces & aliases6

interface User { id: number }

Interface (extendable)

type ID = string | number

Type alias (unions, etc.)

interface A extends B {}

Inheritance

type C = A & B

Intersection

readonly id: number

Immutable property

[key: string]: number

Index signature

Functions & generics5

(a: number, b?: number) => number

Function type (optional param)

function first<T>(arr: T[]): T | undefined

Generics — keep the relationship between input and output types.

<T extends { id: number }>

Generic constraint

function isFoo(x: unknown): x is Foo

Type predicate — a function that narrows a type for the caller.

function f(): asserts x is Foo

Assertion 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 Foo

Class narrowing

keyof T

Union of a type's keys

obj satisfies Config

Check a value matches a type without widening it (4.9+).

enum Color { Red, Green }

Enum

import type { Foo } from "./x"

Type-only import