TypeScript Cheatsheet

Type system survival kit. Assumes TS 5+.

1 credit

Basic types

6 items
Primitives
string | number | boolean | null | undefined | bigint | symbol
Arrays
string[] / Array<string>
Tuples
[string, number] / readonly [string, ...number[]]
Object
{ id: number; name?: string; readonly slug: string }
Function
(a: number, b: number) => number
Literal union
type Status = "idle" | "loading" | "error"

Generics

typescript
function first<T>(arr: T[]): T | undefined {
  return arr[0];
}
first<string>(["a", "b"]); // inferred T = string

type ApiResponse<Data> = { ok: boolean; data: Data; error?: string };

Utility types

8 items
Make optional
Partial<T>
Make required
Required<T>
Read-only
Readonly<T>
Pick keys
Pick<User, "id" | "name">
Exclude keys
Omit<User, "password">
Record
Record<string, number>
Function return type
ReturnType<typeof fn>
Awaited<Promise<T>>
Awaited<T>

Narrowing

4 items
typeof
if (typeof x === "string") { ... }
in operator
if ("email" in user) { ... }
Discriminated union
switch (msg.kind) { case "a": ...; case "b": ... }
Type guard
function isUser(x: unknown): x is User { ... }

Config essentials (tsconfig)

  • `"strict": true` — turn on all strict flags. Always.
  • `"noUncheckedIndexedAccess": true` — arr[i] is T | undefined, catches off-by-one bugs.
  • `"exactOptionalPropertyTypes": true` — `{x?: number}` means "missing", not `undefined`.
  • `"moduleResolution": "bundler"` — if using Vite / Next / esbuild.

Further reading