TypeScript
28 cheat sheets.
TypeScript Project References
Split a TypeScript codebase into composite sub-projects, build them incrementally in dependency order with tsc --build, and integrate the result with bundlers, Turborepo, and Nx.
TypeScript Modules
Deep dive into TypeScript's module system — module/moduleResolution pairs, .mts/.cts file extensions, NodeNext vs Bundler resolution, paths, and tsconfig extends.
TypeScript Compiler API
Drive the TypeScript compiler programmatically — parse SourceFiles, query the type checker, transform ASTs with the factory, and write codemods that scale across a monorepo.
typeof & keyof
TypeScript's typeof promotes a runtime value into the type position; keyof extracts the union of property keys. Together they form the backbone of type-safe lookups, enum-from-object patterns, and inferred shapes.
type-fest
Sindre Sorhus's collection of essential TypeScript utility types — PartialDeep, ReadonlyDeep, SetOptional, RequireAtLeastOne, Merge, Tagged, JsonValue, Opaque, and dozens more — so you don't hand-roll them.
ts-node, tsx & Friends
Compare ts-node, tsx, Node 22.6+ --experimental-strip-types, Bun, and Deno for running .ts files directly; pick the right tool, configure watch mode, and avoid the classic ESM / type-strip pitfalls.
Template Literal Types
Template literal types let TypeScript pattern-match and synthesize string types — covering Uppercase/Lowercase intrinsics, infer-based parsers (Split, Join, CamelCase), route-param extraction, JSON path keys, and typed i18n helpers.
Structural Typing
Understand TypeScript's structural type system — assignability is based on shape, not name; excess property checks are the one exception; nominal typing requires branded types or class privates.
satisfies Operator
TypeScript's satisfies operator checks a value against a type while preserving its narrow literal inferred type. The middle ground between an annotation that widens and an as cast that lies.
React with TypeScript
Type-safe React patterns — function components, prop types, children, event handlers, refs, generic components, polymorphic `as` props, and Zod-powered forms.
infer Keyword
TypeScript's infer keyword declares a fresh type variable inside a conditional type, letting you pull a sub-type out of a matched shape. It powers ReturnType, Parameters, Awaited, and most advanced type extractors.
Discriminated Unions
Discriminated unions model finite states with a shared literal tag, enabling exhaustive narrowing in switch statements, Result-style error handling, reducer actions, and pattern-matched API responses.
Declaration Merging
Deep dive into TypeScript declaration merging — interface merging, namespace + class, namespace + function, module augmentation, global augmentation, and the Express Request pattern.
Branded Types
Branded (a.k.a. nominal, opaque, tagged) types add a phantom marker to a primitive so the compiler refuses to mix two strings — useful for IDs, units, validated input, and money. Covers hand-rolled brands, type-fest's Opaque/Tagged, Zod's .brand(), class-based brands, and runtime validation pairings.
.d.ts Files
Authoring .d.ts files — ambient declarations, declare module 'foo', asset typing (*.svg?raw), declare global, triple-slash refs, and the home-hero.svg?raw pattern used in this project.
Utility Types
TypeScript's built-in generic utility types that transform existing types into new ones. Covers Partial, Required, Readonly, Record, Pick, Omit, Exclude, Extract, ReturnType, Awaited, and more.
TypeScript Installation & Running
Install the TypeScript compiler, run .ts files without a build step using ts-node or tsx, and compile projects with tsc. Covers tsc flags, watch mode, and project references.
TypeScript
Static typing for JavaScript — tsconfig reference, the full type system, generics, utility types, narrowing, and common compiler errors explained.
Types vs Interfaces
TypeScript has two ways to define object shapes — type aliases and interface declarations. Learn when each is appropriate, how they differ in extension, merging, and composability.
Type-Only Imports & Exports
import type and export type erase at compile time, preventing runtime side-effects and enabling better tree-shaking. Covers inline type qualifiers, verbatimModuleSyntax, and common gotchas with enums and namespaces.
Type Narrowing
TypeScript narrowing refines broad types to specific ones within code branches. Covers typeof, instanceof, in, equality, assignment narrowing, discriminated unions, control flow analysis, and the never type.
Type Guards
User-defined type guards narrow types at runtime using the `is` predicate, assertion functions, generic guards, and class-based patterns. Covers API validation, DOM guards, and when to use Zod.
tsconfig.json Reference
Complete reference for tsconfig.json — compiler options for type checking, module resolution, output, paths, and JSX. Includes ready-to-use presets for Node 20, browser libraries, and Vite/React apps.
Mapped & Conditional Types
Mapped types iterate over type keys to create new types; conditional types pick between types based on a condition. Together they power all built-in utility types and advanced type composition.
Generics
TypeScript generics allow writing reusable, type-safe code that works over many types. Covers generic functions, interfaces, classes, constraints, keyof, conditional types, and the infer keyword.
Enums
TypeScript enums create named constant sets as real runtime objects. Covers numeric, string, and const enums; reverse mapping; enum pitfalls; and when to prefer a union of string literals instead.
Decorators
TypeScript decorators annotate and transform classes and class members. Covers the TC39 stage-3 standard decorators (TS 5.0+), legacy experimentalDecorators, decorator factories, method/class/field decorators, and reflect-metadata for NestJS and Angular patterns.
Common TypeScript Errors
A reference for the most frequent TypeScript compiler errors — their meaning, a minimal reproduction, and the correct fix. Covers TS2304, TS2345, TS2339, TS2322, TS2532, TS2554, TS2307, TS2366, TS7006, TS2571, TS2769, TS2693, plus type assertions and ts-expect-error.