TypeScript Cheatsheet — One-Page Quick Reference
TypeScript Cheatsheet
Section titled “TypeScript Cheatsheet”One-page quick reference for TypeScript developers. Bookmark this page for daily use.
1. Primitive & Special Types
Section titled “1. Primitive & Special Types”const str: string = "hello"; // Stringconst num: number = 42; // Number (float, int, hex, binary, octal)const bool: boolean = true; // Booleanconst big: bigint = 9007199254740993n; // BigInt (ES2020)const sym: symbol = Symbol("id"); // Symbol (unique)const und: undefined = undefined; // Not yet assignedconst nul: null = null; // Intentional absenceconst v: void = undefined; // No return valueconst nvr: never = throw Error(); // Never occurs
// Special typeslet anyType: any = 42; anyType = "x"; // Opt-out of checking (avoid!)let unk: unknown = 42; // Type-safe any (must narrow)2. Type Annotations & Inference
Section titled “2. Type Annotations & Inference”// Variable annotationsconst name: string = "Alice"; // Explicitconst age = 25; // Inferred: numberlet items = [1, 2, 3]; // Inferred: number[]
// Function annotationsfunction add(a: number, b: number): number { return a + b; }const multiply = (a: number, b: number): number => a * b;
// Contextual typing (inferred from context)[1, 2, 3].map((n) => n.toFixed(2)); // n inferred as numberdocument.addEventListener("click", (e) => e.clientX); // e inferred as MouseEvent3. Object Types
Section titled “3. Object Types”// Interface (preferred for public API shapes)interface User { readonly id: string; // Readonly name: string; email: string; age?: number; // Optional}
// Type aliastype Point = { x: number; y: number };
// Index signaturestype Dict = { [key: string]: unknown };interface StringDict { [key: string]: string; length: number; }
// Excess property checks (only on object literals)// const u: User = { name: "A", email: "a@b", extra: true }; // ❌ Errorconst userData = { name: "A", email: "a@b", extra: true };const u: User = userData; // ✅ OK (structural)4. Arrays & Tuples
Section titled “4. Arrays & Tuples”// Arraysconst a1: number[] = [1, 2, 3];const a2: Array<string> = ["a", "b"];const a3: readonly number[] = [1, 2, 3]; // Immutableconst a4: (string | number)[] = [1, "x"];
// Tuples (fixed-length, typed positions)const t1: [string, number] = ["age", 25];const t2: [string, number?] = ["hello"]; // Optional elementsconst t3: [start: number, end: number]; // Labeled tuples (TS 4.0+)
// Variadic tuplestype Head = [string, ...number[]]; // string then N numberstype Tail = [...number[], string]; // N numbers then string (TS 4.2+)type Both = [string, ...number[], boolean]; // Mixed
// Real-world: React useStatetype UseStateResult<T> = [T, (value: T) => void];5. Functions
Section titled “5. Functions”// Parameter & return typesfunction greet(name: string, greeting?: string): string {}function create(name: string, role: string = "user"): User {}function sum(...nums: number[]): number {}
// Function type expressionstype Callback = (error: Error | null, result?: unknown) => void;type MathOp = (a: number, b: number) => number;
// Overloadsfunction parse(input: string): string[];function parse(input: number): number;function parse(input: string | number): string[] | number { return typeof input === "string" ? input.split(",") : input * 2;}
// Call signatures (callable + properties)interface Counter { (start: number): string; interval: number; reset(): void;}
// Construct signaturestype Ctor = new (name: string) => Animal;6. Union & Intersection Types
Section titled “6. Union & Intersection Types”// Union — "either/or"type Status = "active" | "inactive" | "pending";type ID = string | number;type Result = string | number | boolean;
// Intersection — "all combined"type Admin = User & { role: "admin"; permissions: string[] };type NamedPoint = { name: string } & Point;
// Narrowing techniquesif (typeof x === "string") {} // typeofif (x instanceof Date) {} // instanceofif ("fly" in animal) {} // in operatorif (x === null) {} // Equalityif (x) {} // Truthiness
// Discriminated union (most powerful pattern)type Shape = | { kind: "circle"; radius: number } | { kind: "square"; side: number };function getArea(s: Shape): number { switch (s.kind) { case "circle": return Math.PI * s.radius ** 2; case "square": return s.side * s.side; }}7. Type Operators
Section titled “7. Type Operators”// keyof — get keys as uniontype UserKeys = keyof User; // "id" | "name" | "email"
// typeof — get value's type (used with ReturnType etc.)const config = { url: "https://api.com" };type Config = typeof config; // { url: string }
// Indexed access — T[K]type UserName = User["name"]; // stringtype NameOrAge = User["name" | "age"]; // string | numbertype AllValues = User[keyof User]; // string | number | undefined
// as const — preserve literal typesconst Colors = { red: "#ff0000", blue: "#0000ff" } as const;// Colors.red is "#ff0000" (literal), not string
// satisfies — validate type + preserve literal (TS 4.9+)const palette = { red: [255, 0, 0] } satisfies Record<string, [number, number, number]>;8. Interfaces
Section titled “8. Interfaces”// Extendinginterface Animal { name: string; }interface Dog extends Animal { breed: string; }
// Multiple inheritanceinterface Duck extends Flyable, Swimmable { quack(): void; }
// Declaration merging (interfaces only!)interface User { name: string; }interface User { age: number; } // Merged: User has name + age
// Interface vs Typeinterface type───────────────────────────────── ────────────────────────────────✅ Declaration merging ❌ No merging✅ Extends (extend keyword) ✅ Intersection (&)❌ Union types ✅ Union types (|)❌ Mapped/conditional types ✅ Mapped/conditional types❌ Primitive aliases ✅ Primitive aliases❌ Tuple types ✅ Tuple types9. Generics
Section titled “9. Generics”// Generic functionfunction identity<T>(arg: T): T { return arg; }const str = identity("hello"); // inferred: stringconst num = identity<number>(42); // explicit: number
// Generic constraintsinterface HasLength { length: number; }function logLength<T extends HasLength>(arg: T): T { console.log(arg.length); return arg;}
// keyof constraintfunction getProp<T, K extends keyof T>(obj: T, key: K): T[K] { return obj[key];}
// Generic interfaceinterface Repository<T> { get(id: string): Promise<T | null>; getAll(): Promise<T[]>; create(data: Omit<T, "id">): Promise<T>;}
// Generic classclass ApiRepo<T extends { id: string }> implements Repository<T> { async get(id: string): Promise<T | null> { /* ... */ }}
// Multiple type parametersfunction pair<A, B>(a: A, b: B): [A, B] { return [a, b]; }10. Mapped Types
Section titled “10. Mapped Types”// Basic mapped typetype Readonly<T> = { readonly [K in keyof T]: T[K] };type Optional<T> = { [K in keyof T]?: T[K] };type Nullable<T> = { [K in keyof T]: T[K] | null };
// Modifierstype Required<T> = { [K in keyof T]-?: T[K] }; // Remove optionaltype NonReadonly<T> = { -readonly [K in keyof T]: T[K] }; // Remove readonly
// Key remapping with `as` (TS 4.1+)type Getters<T> = { [K in keyof T as `get${Capitalize<string & K>}`]: () => T[K];};
// Filtering keystype StringKeys<T> = { [K in keyof T as T[K] extends string ? K : never]: T[K];};
// Template literal keystype Events<T> = { [K in keyof T as `on${Capitalize<string & K>}`]: (data: T[K]) => void;};
// Homomorphic (preserves modifiers)// { [K in keyof T]: ... } — preserves readonly/optional// { [P in K]: ... } — doesn't preserve modifiers (K is any key union)11. Conditional Types
Section titled “11. Conditional Types”// Basic syntaxtype IsString<T> = T extends string ? true : false;type A = IsString<"hello">; // truetype B = IsString<42>; // false
// Filtering with never (removed from unions)type NonNullable<T> = T extends null | undefined ? never : T;type C = NonNullable<string | null>; // string
// infer keyword — extract typestype ReturnType<T> = T extends (...args: any[]) => infer R ? R : never;type D = ReturnType<() => string>; // string
type ElementType<T> = T extends (infer U)[] ? U : never;type E = ElementType<string[]>; // string
type UnwrapPromise<T> = T extends Promise<infer U> ? UnwrapPromise<U> : T;type F = UnwrapPromise<Promise<Promise<string>>>; // string
type FirstParam<T> = T extends (first: infer F, ...args: any[]) => any ? F : never;type G = FirstParam<(name: string, age: number) => void>; // string
// Distributive conditional types// T extends U ? X : Y distributes over unions of Ttype ToArray<T> = T extends any ? T[] : never;type H = ToArray<string | number>; // string[] | number[]// To prevent distribution, wrap in [T]:type ToArray2<T> = [T] extends [any] ? T[] : never;type I = ToArray2<string | number>; // (string | number)[]12. Utility Types (Complete Reference)
Section titled “12. Utility Types (Complete Reference)”interface User { id: string; name: string; email: string; age: number; }
// ─── Object Transformations ───type P = Partial<User>; // All optionaltype R = Required<User>; // All requiredtype Ro = Readonly<User>; // All readonlytype Pi = Pick<User, "id" | "name">; // { id: string; name: string }type O = Omit<User, "email">; // { id: string; name: string; age: number }
// ─── Dictionary ───type Rec = Record<"a" | "b", string>; // { a: string; b: string }
// ─── Union Manipulation ───type Ex = Exclude<"a" | "b" | "c", "a">; // "b" | "c"type Ext = Extract<"a" | "b" | "c", "a" | "c">; // "a" | "c"type NN = NonNullable<string | null | undefined>; // string
// ─── Function Introspection ───function fn(name: string, age: number): User { return {} as User; }type RT = ReturnType<typeof fn>; // Usertype P2 = Parameters<typeof fn>; // [name: string, age: number]type CP = ConstructorParameters<new (n: string) => User>; // [n: string]type IT = InstanceType<new (n: string) => User>; // User
// ─── `this` Introspection ───type TPT = ThisParameterType<(this: Window, x: number) => void>; // Windowtype OPT = OmitThisParameter<(this: Window, x: number) => void>; // (x: number) => void
// ─── Promise ───type Aw = Awaited<Promise<Promise<string>>>; // string13. Type Guards & Narrowing
Section titled “13. Type Guards & Narrowing”// Built-in guardstypeof x === "string" | "number" | "boolean" | "bigint" | "symbol" | "undefined" | "function" | "object"x instanceof Date | Array | RegExp | Map | Set"property" in objArray.isArray(x)x === null / x === undefinedx ?? "default" // Nullish coalescing (null/undefined only)
// User-defined type guardfunction isUser(x: unknown): x is User { return !!x && typeof x === "object" && "name" in x;}
// Assertion function (throws on failure)function assertIsString(v: unknown): asserts v is string { if (typeof v !== "string") throw new Error("Not a string");}
// Generic assertionfunction assert(condition: unknown, msg: string): asserts condition { if (!condition) throw new Error(msg);}
// Exhaustiveness checkfunction assertNever(x: never): never { throw new Error(`Unexpected: ${x}`);}14. Template Literal Types
Section titled “14. Template Literal Types”// Basic combinationtype Color = "red" | "green" | "blue";type Size = "sm" | "md" | "lg";type ColorSize = `${Color}-${Size}`; // "red-sm" | "red-md" | ... | "blue-lg"
// Intrinsic string typestype Cap = Capitalize<"hello">; // "Hello"type Uncap = Uncapitalize<"Hello">; // "hello"type Up = Uppercase<"hello">; // "HELLO"type Low = Lowercase<"HELLO">; // "hello"
// Pattern matching with infertype ExtractRoute<T> = T extends `/api/${infer R}` ? R : never;type R = ExtractRoute<"/api/users">; // "users"
// CSS valuestype CSSVal = `${number}${"px" | "rem" | "em" | "%"}`; // "10px" | "2rem" | ...
// Key remapping utilitytype EventName = `on${Capitalize<string>}`; // "onChange" | "onClick" | ...15. Branded (Nominal) Types
Section titled “15. Branded (Nominal) Types”// Simple brand (compile-time only)type Brand<T, B> = T & { __brand: B };type UserId = Brand<string, "UserId">;type PostId = Brand<string, "PostId">;function getUser(id: UserId): void {}function getPost(id: PostId): void {}getUser("abc" as UserId); // ✅getUser("xyz" as PostId); // ❌ Compile error!
// Opaque type (safer brand with unique symbol)declare const _brand: unique symbol;type Opaque<T, B> = T & { readonly [_brand]: B };type Email = Opaque<string, "Email">;function createEmail(s: string): Email { if (!s.includes("@")) throw new Error("Invalid email"); return s as Email;}16. Common Patterns
Section titled “16. Common Patterns”// ─── Discriminated Union + API Result ───type ApiResult<T> = | { status: "success"; data: T } | { status: "error"; error: string } | { status: "loading" };
function handleResult<T>(r: ApiResult<T>) { switch (r.status) { case "success": return r.data; case "error": return r.error; case "loading": return "Loading..."; }}
// ─── Result Type (Rust-style) ───type Result<T, E = Error> = | { ok: true; value: T } | { ok: false; error: E };
// ─── Deep Partial ───type DeepPartial<T> = { [K in keyof T]?: T[K] extends object ? DeepPartial<T[K]> : T[K];};
// ─── Deep Readonly ───type DeepReadonly<T> = { readonly [K in keyof T]: T[K] extends Record<string, unknown> ? DeepReadonly<T[K]> : T[K];};
// ─── Type-safe Event Emitter ───type EventMap = { userCreated: { id: string; name: string }; error: { message: string; code: number };};class Emitter<T extends Record<string, unknown>> { on<K extends keyof T>(event: K, handler: (data: T[K]) => void): void {} emit<K extends keyof T>(event: K, data: T[K]): void {}}
// ─── Singleton Pattern ───class ServiceRegistry { private static services = new Map<string, unknown>(); static register<T>(name: string, service: T): void { this.services.set(name, service); } static get<T>(name: string): T { return this.services.get(name) as T; }}
// ─── Fluent Builder ───class QueryBuilder<T> { private conditions: string[] = []; where(c: string): this { this.conditions.push(c); return this; } limit(n: number): this { /* ... */ return this; } execute(): Promise<T[]> { /* ... */ return [] as any; }}
// ─── Enum Alternative (union of literals) ───type Direction = "north" | "south" | "east" | "west";const Direction = { North: "north" as const, South: "south" as const, East: "east" as const, West: "west" as const,};
// ─── Type-safe Object.keys ───function typedKeys<T extends Record<string, unknown>>(obj: T): (keyof T)[] { return Object.keys(obj) as (keyof T)[];}
// ─── Custom Error Classes ───class AppError extends Error { constructor(message: string, public code: string, public statusCode: number = 500) { super(message); this.name = "AppError"; }}17. tsconfig Quick Reference
Section titled “17. tsconfig Quick Reference”{ "compilerOptions": { "strict": true, // Enable ALL strict checks (recommended) "target": "ES2022", // JS output version "module": "ESNext", // Module system "moduleResolution": "bundler", // Module resolution strategy "outDir": "./dist", // Output directory "rootDir": "./src", // Source directory "esModuleInterop": true, // CJS/ESM compatibility "skipLibCheck": true, // Skip node_modules type checks (faster) "declaration": true, // Generate .d.ts files "sourceMap": true // Debugging support }}Strict mode enables: strictNullChecks, strictFunctionTypes, strictBindCallApply, strictPropertyInitialization, noImplicitAny, noImplicitThis, alwaysStrict
Pro Tip: Bookmark this page. Use
Ctrl+F/Cmd+Fto quickly find any concept. For deeper explanations, visit the individual TypeScript doc pages in the sidebar.