TypeScript Types Quick Reference
The type-system features you actually use — unions, generics, utility types, narrowing, and the syntax that trips people up.
Building blocks
- Union / literal
type Dir = "n" | "s" | "e" | "w"- Interface vs type
- interface for object shapes (extendable); type for unions/aliases
- Optional / readonly
{ name?: string; readonly id: number }- Function type
type Fn = (a: number) => string- Array / tuple
number[]·[string, number]
Generics
- Generic function
function first<T>(xs: T[]): T { return xs[0]; }- Constraint
<T extends { id: number }>- Default
<T = string>
Utility types
- Partial / Required
Partial<T>all optional ·Required<T>all required- Pick / Omit
Pick<T, "a" | "b">·Omit<T, "id">- Record
Record<string, number>— a keyed map- Return / Parameters
ReturnType<typeof f>·Parameters<typeof f>- Readonly / NonNullable
Readonly<T>·NonNullable<T>
Narrowing & escape hatches
- typeof / in / instanceof
if (typeof x === "string")narrows the type- Type guard
function isCat(a: Animal): a is Cat- Non-null assertion
el!.focus()— "trust me, not null" (use sparingly)- as const
const dirs = ["n","s"] as const— literal, readonly tuple- unknown vs any
- Prefer
unknown— forces a check before use;anydisables type-checking