TypeScript Monorepo: Project References Explained
Split a monorepo into TypeScript projects with references and composite, build with tsc -b, and keep editor and CI fast.
In this chapter
Monorepos and project references
More on The compiler
Keep learning
Overview
rebuilt (source changed) rebuilt (.d.ts it reads changed) timestamps only: .d.ts unchanged timestamps only, unless its own deps’ .d.ts changed not touched Package you edit Kind of change add an export chapters · the compiler Monorepos and project references Split a big repo into small TypeScript projects that build in order and reuse each other's .d.ts files. A monorepo is one repository holding many packages. Here a project means one folder with its own tsconfig.json. A .d.ts file (declaration file) holds only types, no code.
1. The problem: one package reaching into another's source
The checkout package imports money by a relative path straight into money's src folder. Its tsconfig only covers checkout/src.
packages/checkout/src/total.ts
File '…/money/src/index.ts' is not under 'rootDir' '…/checkout/src'. File '…/money/src/index.ts' is not listed within the file list of project '…/checkout/tsconfig.json'. Error 1: rootDir is the one folder that must hold all of this project's source. money's file lives outside it. Error 2: the file is not in this project's include list. Even if you silence both, the problems below remain. Checked twice money is re-checked inside every package that imports it. No boundary Any file can import any other file’s internals. All or nothing One edit, and the whole repo is checked again.
packages/checkout/tsconfig.json
composite: true Turns on declaration (write .d.ts files) and incremental (remember the last build). Every file must match include or files. declarationMap: true Go to Definition opens money/src, not the .d.ts. checkout reads money’s dist/*.d.ts instead of re-checking its source. Builds money first, then checkout. Skips what is up to date. tsc says reference a project without composite TS6306: Referenced project … must have setting "composite": true. Only composite projects promise to write the .d.ts files others read. import a file outside your include TS6307: File … is not listed within the file list of project. import ../other-package/src directly TS6059: File … is not under 'rootDir'. Import the package by name instead (section 5). declarationMap without composite TS5069: Option 'declarationMap' cannot be specified without … 'declaration' or 'composite'. All four reproduced with tsc 5.6 -b.
4. tsc -b: build mode
read references build a project graph leaves first: money → checkout inputs vs .tsbuildinfo build only stale emit .js + .d.ts
tsc -b --verbose after editing a function body in money
Projects in this build: money, checkout, tsconfig.json out of date: tsconfig.tsbuildinfo is older than src/index.ts Building project packages/money … up to date with .d.ts files from its dependencies Updating output timestamps … (no type check) Try it: edit one package and see how far the rebuild spreads. A leaf package uses no other package. “Timestamps only” means tsc just marks the project as up to date, without checking it again. What does tsc -b do with checkout? updates timestamps only rebuilds checkout error: checkout is stale
5. paths or references?
paths alias "@shop/money": ["packages/money/src"] in tsconfig paths maps an import name to a folder. It only renames an import. All packages land in one program and are checked together. references + workspaces npm/pnpm workspaces (a package-manager feature for monorepos) link @shop/money into node_modules. Its package.json exports.types tells TypeScript where its types live: dist/index.d.ts.
checkout imports by package name; types cross the boundary
Argument of type 'number' is not assignable to parameter of type 'Cents'. Why line 4 fails: in money, Cents is a branded number (a number with an invisible tag), and only cents() makes one. A plain 1999 does not fit. checkout learns this rule from money's .d.ts, without reading money's source.
Remember
Referenced projects need composite: true. Consumers read the .d.ts, not the source, so each package is checked once. tsc -b builds in dependency order and skips up-to-date projects. Add declarationMap so Go to Definition lands in source. ! Cheat code composite in every package, references to what it imports, and tsc -b instead of tsc. → Go deeper: slow types, inside the compiler, declaration files, modules and ESM.
More courses
- Jobs, linked to lessons — Browse jobs where every requirement links to the chapter that teaches it. See how ready you are, how long the gap takes to close, and which listings look stale.
- Match a job post to courses — Paste a job post. Each skill is matched to a track here and put in learning order, foundations first.
- React patterns from open source — Patterns extracted from Material UI, Radix, React Aria, React Hook Form, Redux Toolkit, TanStack, and Next.js — not slides about hooks.
- Databases in depth — How a query becomes work. Indexes, isolation, locks, and the plans that explain why a page is slow — not an ORM tour.
- System Design — Core concepts first (SQL vs NoSQL, CAP, indexing, replication, sharding), then designs grouped by what carries them: the database, Redis, Kafka, or many services.
- NestJS: build a real API — One ticket-booking API, built in order: a plain Node server first, then NestJS modules, validation, a real database, and the features real clients lean on.
- Microservices — Microservice patterns one diagram at a time: how to split a monolith, how services talk, and how data stays consistent without distributed transactions.
- Kafka — A beginner-to-senior path through event logs, reliable delivery, production operations, and disaster recovery.
- AI engineering for software engineers — Eight free reads, in order, for software engineers moving into AI: when to build an agent, how to build one, how to test it, and how the model works underneath.
- Frontend system design — How a UI is sliced when it is a product, not a page: ownership of state, cache, routes, and what happens when the network lies.
- CSS in depth — See the box, then the page. Visual chapters from the box model through Flexbox surprises, Grid, selectors, and units, then backgrounds, shapes, and visual effects, each with a playground.
- Accessibility — Foundations first (WCAG in plain words, semantic HTML, lists, alt text), then hands-on HTML labs, a full pass of WAI-ARIA 1.3, and a final fix-the-page challenge.
- Docker — What an image is, why layers cache, and how containers find each other on a network.
- Redis — Fifteen Redis patterns, one picture each: cache, session, lock, counter, rate limiter, queue, and when Redis is the wrong tool.
- Kubernetes — Kubernetes from zero to operators: why it exists, Pods, Deployments and Services, then configuration, networking, storage, scheduling, scaling and security.
- AWS — AWS networking in plain English. A VPC is a gated housing society: streets are subnets, the Internet Gateway is the main gate, and a route table is the signboard.
- Networking: the NetRoute game — NetRoute is a packet logistics game. You wire up computers, switches, routers, firewalls and gateways, set each device, and watch glowing packets travel. If a packet gets dropped, the game shows you which header field was wrong.