Fast reference for everyday TypeScript typing (4.x style).
Annotate basics
let n: number;
const user = { id: 1, name: "Ada" } as const;
type Role = "admin" | "member";
Interfaces vs types
interface User { id: string; name: string }
type UserMap = Record<string, User>;
Utility types
type A = Partial<User>; // all optional
type B = Required<A>;
type C = Readonly<User>;
type D = Pick<User, "id" | "name">;
type E = Omit<User, "name">;
type F = Record<string, number>;
type G = Exclude<"a"|"b"|"c", "a">;
type H = Extract<string|number, string>;
type I = NonNullable<string | undefined>;
keyof, typeof, indexed access
type UserKeys = keyof User;
const config = { api: "https://x", timeout: 5000 } as const;
type Config = typeof config;
type ApiUrl = (typeof config)["api"];
Generics
function first<T>(arr: T[]): T | undefined {
return arr[0];
}
satisfies (narrowing without widening)
const palette = {
red: [255, 0, 0],
green: "#00ff00",
} satisfies Record<string, string | [number, number, number]>;
Use unknown instead of any at boundaries; narrow with typeof/instanceof or zod.