TypeScript Cheat Sheet
TypeScript types, interfaces, generics, and advanced type patterns.
3 PagesIntermediateMay 8, 2026
Basic Types
Annotate variables with primitive types.
typescript
let id: number = 5;let name: string = "SkillVeris";let active: boolean = true;let tags: string[] = ["a", "b"];let tuple: [string, number] = ["age", 30];
Interfaces
Define the shape of an object.
typescript
interface User { id: number; name: string; email?: string; // optional readonly createdAt: Date;}function greet(u: User): string { return `Hello, ${u.name}`;}
Generics
Write reusable, type-safe functions.
typescript
function identity<T>(value: T): T { return value;}function firstItem<T>(arr: T[]): T | undefined { return arr[0];}
Utility Types
Built-in type transformations.
- Partial<T>- All properties optional
- Required<T>- All properties required
- Pick<T,K>- Subset of keys
- Omit<T,K>- Exclude keys
- Record<K,V>- Object type with keys K, values V
Unions & Narrowing
Discriminated unions and type guards.
typescript
type Shape = | { kind: "circle"; r: number } | { kind: "square"; side: number };function area(s: Shape): number { switch (s.kind) { case "circle": return Math.PI * s.r ** 2; case "square": return s.side ** 2; default: { const _exhaustive: never = s; // compile-time exhaustiveness return _exhaustive; } }}
Type Guards & Assertions
Custom guards, assertion functions, and narrowing operators.
typescript
// User-defined type guardfunction isString(x: unknown): x is string { return typeof x === "string";}// Assertion functionfunction assert(cond: unknown, msg: string): asserts cond { if (!cond) throw new Error(msg);}const val = getValue() as string; // assertion (unchecked)const el = document.querySelector("a")!; // non-null assertionif ("name" in obj) { /* in-operator narrowing */ }
Mapped & Conditional Types
Build types programmatically with infer and key remapping.
typescript
// Conditional with infertype ElementType<T> = T extends (infer U)[] ? U : T;type UnwrapPromise<T> = T extends Promise<infer U> ? U : T;// Mapped type with key remappingtype Getters<T> = { [K in keyof T as `get${Capitalize<string & K>}`]: () => T[K];};type G = Getters<{ name: string }>; // { getName: () => string }
Key tsconfig Flags
Compiler options that most affect type safety.
- strict- enables the full strict family (noImplicitAny, strictNullChecks, etc.)
- noUncheckedIndexedAccess- adds undefined to indexed access like arr[i] to force bounds checks
- exactOptionalPropertyTypes- distinguishes missing keys from keys explicitly set to undefined
- noImplicitOverride- requires the override keyword when redefining a base-class method
- verbatimModuleSyntax- enforces explicit import type / export type for erasable imports
- moduleResolution: bundler- resolution mode matching modern bundlers (Vite, esbuild)
Pro Tip
Enable "strict": true in tsconfig.json from day one — retrofitting strict mode later is far more painful.
Was this cheat sheet helpful?
Explore Topics
#TypeScript#TypeScriptCheatSheet#Programming#Intermediate#BasicTypes#Interfaces#Generics#UtilityTypes#CheatSheet#SkillVeris