TypeScript Cheatsheet
TypeScript types, interfaces, generics, utility types, and advanced patterns for safer code.
TypeScript adds static typing to JavaScript, catching bugs at compile time and providing better IDE tooling. This cheatsheet covers basic types, interfaces, generics, discriminated unions, utility types, and common patterns.
All examples target TypeScript 5.x with strict mode enabled.
Basic Types & Annotations
let name: string = "Alice"const items: string[] = ["a", "b"]const pair: [string, number] = ["x", 1]let value: string | null = nullInterfaces & Type Aliases
interface User { name: string; age: number }type Point = { x: number; y: number }Generics
interface Response<T> { data: T; error?: string }function identity<T>(arg: T): T { return arg }type Result<T> = T extends string ? "str" : "other"Union & Intersection Types
type Status = "active" | "inactive" | "pending"type Combined = A & Bif (typeof x === "string") { x.toUpperCase() }Utility Types
Partial<T>Required<T>Pick<T, K>Omit<T, K>Record<K, V>ReturnType<typeof fn>Advanced Patterns
as constsatisfies operatorTypeScript Cheatsheet
TypeScript types, interfaces, generics, utility types, and advanced patterns for safer code.
TypeScript adds static typing to JavaScript, catching bugs at compile time and providing better IDE tooling. This cheatsheet covers basic types, interfaces, generics, discriminated unions, utility types, and common patterns.
All examples target TypeScript 5.x with strict mode enabled.
Basic Types & Annotations
let name: string = "Alice" — Type annotation after colon. :string, :number, :boolean, etc.const items: string[] = ["a", "b"] — Array type — string[], number[], or Array<string>.const pair: [string, number] = ["x", 1] — Tuple — fixed-length array with typed positions.let value: string | null = null — Union type — can be one of several types.Interfaces & Type Aliases
interface User { name: string; age: number } — Interface — defines the shape of an object. Can be extended.type Point = { x: number; y: number } — Type alias — works for primitives, unions, and intersections too.Generics
interface Response<T> { data: T; error?: string } — Generic interface — the type is parameterized and passed in.function identity<T>(arg: T): T { return arg } — Generic function — captures the type from the argument.type Result<T> = T extends string ? "str" : "other" — Conditional type — selects type based on a condition.Union & Intersection Types
type Status = "active" | "inactive" | "pending" — Union of string literals — restricts to exact values.type Combined = A & B — Intersection type — combines all properties of A and B.if (typeof x === "string") { x.toUpperCase() } — Type narrowing — TypeScript narrows type inside the guard.Utility Types
Partial<T> — Make all properties of T optional.Required<T> — Make all properties of T required.Pick<T, K> — Create a type with only the specified keys from T.Omit<T, K> — Create a type without the specified keys.Record<K, V> — Create an object type with keys K and values V.ReturnType<typeof fn> — Extract the return type of a function.Advanced Patterns
as const — Mark a value as deeply read-only with literal types.satisfies operator — Check that a value matches a type without widening it (TS 4.9+).