TypeScript Project References were built for a problem that only becomes painful after your monorepo has already grown past the point where a single tsconfig.json is tenable. I learned this the hard way on a platform with about 400k lines of TypeScript split across a design system, three Next.js apps, and a handful of domain packages. Our initial setup used path aliases and a root config with exclude lists. Type checking took 90+ seconds on a developer laptop, incremental edits were not much faster, and nothing prevented an app from deep-importing into another app's internals. Moving to project references didn't just make builds faster — it forced us to define real module boundaries that the compiler could actually enforce. This article covers how that system works under the hood, how to structure it so it helps rather than hinders, and where it still falls short.
Why a Single tsconfig.json Crumbles When Your Monorepo Grows
A root-level tsconfig.json treats your entire monorepo as one program. The compiler resolves every file into a single graph, type-checks everything together, and emits it as one unit. That model works for a small codebase, but it scales linearly at best. Every file you add increases the work the type checker must do on every run, even if you only touched one package.
In my experience, the first symptom is not build time but editor performance. With one program, the TypeScript language server watches every file matched by include. In a monorepo with 8 packages and shared path aliases like @acme/*, a change in packages/utils invalidates the whole program. Autocomplete lags, go-to-definition jumps to stale declaration files, and refactoring becomes unreliable because the compiler has no notion of ownership between packages.
The second failure is architectural. Path aliases give you convenient imports, but they do not create boundaries. Nothing stops apps/web from importing ../../../packages/billing/src/internal/calculateProration.ts directly, bypassing the public API of @acme/billing. You can try to enforce boundaries with ESLint's no-restricted-imports or import/no-relative-parent-imports, but those are lint-time heuristics. They do not understand the build graph, and they drift out of sync with tsconfig.json.
The Cost of Re-Checking Unchanged Code
When you run tsc --noEmit from the root, TypeScript has no cache to reuse. It re-parses and re-checks every file, even if 95% of the codebase hasn't changed. Solutions like incremental: true at the root help a little, but the cache is still monolithic. A single .tsbuildinfo file for 5000 modules becomes large, slow to read, and fragile to invalidate.
Project references solve this by splitting the single program into multiple programs that reference each other explicitly. Each project is checked in isolation against its own dependencies' declaration files (.d.ts), not their source. That isolation is what unlocks both performance and encapsulation.
How Project References Reframe Module Resolution
Under project references, you stop thinking about files and start thinking about projects as build units. A project is a tsconfig.json with composite: true. A reference is a directed edge from one project to another. The compiler uses that graph to determine build order, caching granularity, and — critically — what imports are legal. If apps/web does not reference packages/billing, an import from @acme/billing is an error even if the path resolves on disk. That is a stronger guarantee than any lint rule because it comes from the type checker itself, and it aligns closely with how native MDN Web Docs describes JavaScript modules as having explicit imports and isolated scopes.
Architecting the Reference Graph to Mirror Real Domain Boundaries
The most common mistake I see teams make is mirroring their folder structure one-to-one with project references without first deciding where domain boundaries should be. Your reference graph should reflect team ownership and reuse patterns, not just directories. In one codebase I audited, every package referenced every other package "just in case," which recreated the single-program problem with extra configuration overhead.
Start by classifying packages into layers: foundation (e.g., @acme/tsconfig, @acme/utils), domain (e.g., @acme/billing, @acme/auth), and application (apps/web, apps/admin). References should flow downward only. Apps can reference domain and foundation, domain can reference foundation, but foundation never references domain. This directed acyclic graph (DAG) is what makes incremental builds possible and prevents circular build dependencies.
I've found that a good test for a boundary is whether you could publish the package independently. If packages/ui cannot build without pulling in apps/web, the boundary is inverted. Project references make that inversion a compile-time error, which is exactly what you want.
// tsconfig.json at the repository root
{
"files": [],
"references": [
{ "path": "packages/utils" },
{ "path": "packages/ui" },
{ "path": "packages/billing" },
{ "path": "packages/auth" },
{ "path": "apps/web" },
{ "path": "apps/admin" }
]
}
// packages/billing/tsconfig.json
{
"extends": "@acme/tsconfig/base.json",
"compilerOptions": {
"composite": true,
"declaration": true,
"declarationMap": true,
"rootDir": "./src",
"outDir": "./dist",
"strict": true
},
"include": ["src/**/*"],
"references": [
{ "path": "../utils" }
]
}
In this setup, billing explicitly depends on utils. If someone in billing tries to import from @acme/ui without adding a reference, TypeScript will error with File is not listed within the file list of project or a module resolution failure. That error is the boundary enforcement working as intended. For shared type modeling, pairing this structure with Discriminated Unions in TypeScript: Modeling Domain Logic Safely helps keep domain packages focused on a well-defined set of states rather than leaking app-specific types downward.
Grouping Versus Granularity in Package Design
There is a tradeoff between having many small projects and a few large ones. Each project adds overhead: a .tsbuildinfo file, declaration emit, and a node in the build graph. In my experience, a sweet spot for most product teams is 8-15 projects in a monorepo. If you have 40 micro-packages that each contain two files, the overhead of declaration maps and cross-project file watching can outweigh the incremental build savings. Conversely, if you have a single packages/shared with 50k lines, you have not gained any isolation. Split that package by domain concern, not by file count.
Composite Mode, Declaration Emit, and the Contract Between Projects
Setting composite: true implies a set of stricter compiler options, and understanding them prevents a lot of configuration thrashing. When composite is enabled, TypeScript requires declaration: true, forces strict handling of certain path settings, and constrains rootDir and outDir so that output structure is predictable. It also enforces that all files in the program are either root files or dependencies of a referenced project. You cannot have loose files floating outside the graph.
The key insight is that projects do not import each other's source. They import emitted .d.ts files. When apps/web imports from @acme/billing, the compiler resolves it to packages/billing/dist/index.d.ts, not packages/billing/src/index.ts. This is why incremental builds can be fast: if only apps/web changed, the compiler does not need to re-check packages/billing/src at all. It just reuses the previously emitted declarations.
That contract has practical consequences. First, you must emit declarations, which means your package.json for internal packages should point main and types at the dist output. Second, declarationMap: true is essential for developer experience. Without it, go-to-definition from an app jumps to the .d.ts file, not the source. With it, the IDE follows the map back to the original .ts file even though the import is technically resolving to dist.
// packages/utils/tsconfig.json - a leaf project with no references
{
"extends": "@acme/tsconfig/base.json",
"compilerOptions": {
"composite": true,
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"outDir": "./dist",
"rootDir": "./src",
"noErrorTruncation": true
},
"include": ["src/**/*"]
}
// apps/web/tsconfig.json - an application that consumes packages
{
"extends": "@acme/tsconfig/base.json",
"compilerOptions": {
"composite": true,
"outDir": "./dist",
"rootDir": "./src",
"noUncheckedIndexedAccess": true
},
"include": ["src/**/*"],
"references": [
{ "path": "../../packages/utils" },
{ "path": "../../packages/ui" },
{ "path": "../../packages/billing" }
]
}
Notice that apps/web also has composite: true even though nothing references it. Making every project composite keeps the graph consistent and lets you use tsc --build uniformly. If you are centralizing compiler options, keep a shared @acme/tsconfig/base.json and avoid overriding strictness per-project. Mixing strict: false in one package and strict: true in another that references it creates confusing errors where a type is strict in the consumer but loose in the producer. I have had more success migrating the whole graph to strict together, using the approach described in Migrating to TypeScript Strict Mode: A Step-by-Step Approach, rather than attempting per-project strictness.
Controlling the Public API Surface
Because consumers only see your .d.ts files, your src/index.ts becomes an explicit public API. I recommend re-exporting only what is intended for external use and keeping internal helpers out of the index. Combine this with utility types to shape that surface carefully. For example, if your billing package needs to expose a sanitized version of an internal model, TypeScript Utility Types: Pick, Omit, Partial and Record Explained is a good reference for using Pick and Omit to derive public types without duplicating definitions. The compiler will not warn you if you export too much, but project references give you the boundary where such discipline pays off.
Incremental Build Orchestration: How tsc --build Transforms Local and CI Performance
The performance gain from project references does not come from references alone. It comes from tsc --build (or tsc -b), which is a different build orchestrator than plain tsc. When you run tsc -b, TypeScript reads the reference graph, topologically sorts it, and builds projects in the correct order, using .tsbuildinfo files to skip projects whose inputs and dependency outputs have not changed.
In a pre-references setup, tsc --noEmit on our codebase took 92 seconds cold and 88 seconds warm — barely any caching. After splitting into 9 composite projects, tsc -b took 41 seconds cold and 4-7 seconds when only one leaf app changed. The warm case is what matters for developer iteration and for CI with a cache. The difference is that tsc -b can prove that 8 projects are unaffected and skip them entirely.
There are a few non-obvious details to get right. First, each project needs incremental: true explicitly or implicitly via composite, and you must allow the compiler to write .tsbuildinfo (default is next to tsconfig.json). Add *.tsbuildinfo to .gitignore but persist it in CI caching. If you cache dist/ and *.tsbuildinfo in GitHub Actions or Buildkite, subsequent CI runs can skip unchanged packages just like local runs do.
{
"name": "ci-build",
"scripts": {
"build": "tsc -b",
"build:force": "tsc -b --force",
"build:verbose": "tsc -b --verbose",
"check": "tsc -b --verbose",
"clean": "tsc -b --clean"
}
}
# GitHub Actions cache example
# - name: Cache TypeScript build info
# uses: actions/cache@v4
# with:
# path: |
# **/dist
# **/*.tsbuildinfo
# key: tsc-${{ runner.os }}-${{ hashFiles('**/tsconfig.json', '**/src/**/*') }}
Watch Mode and Build Information Files
For local development, tsc -b --watch is more efficient than running tsc --watch in each package independently. The build orchestrator watches the graph and only rebuilds downstream projects when an upstream's .d.ts actually changes. If you change a comment in packages/utils, apps that depend on it may not need to be re-checked if the declaration output is identical. This content hashing is why declaration emit is not just an artifact — it is the mechanism for cache invalidation.
One pitfall: deleting dist without deleting .tsbuildinfo leaves the build in an inconsistent state where the cache thinks outputs exist. Always use tsc -b --clean to remove outputs, or add a clean script that removes both dist and .tsbuildinfo. I've found that adding tsc -b --clean && tsc -b --verbose as a diagnostic command helps onboarding — --verbose logs which projects were determined to be up-to-date versus rebuilt, which demystifies the caching.
Diagnosing Broken Graphs: Path Mapping, Circular References, and Editor Blind Spots
Project references are strict, and the error messages are not always intuitive. The most frequent issue is a mismatch between paths in tsconfig.json and the actual reference graph. Developers add a path alias like "@acme/billing": ["packages/billing/src/index.ts"] to make imports resolve in the editor, but that alias bypasses the reference system entirely. The file resolves, but the project is not in the consumer's references, so builds fail or editor state diverges from tsc -b.
The fix is to point path mappings at the built output or, better, to let Node module resolution handle it via package.json names. If packages/billing/package.json has "name": "@acme/billing" and your monorepo uses npm/yarn/pnpm workspaces, you should not need paths at all for internal packages. Let the package manager symlink and let TypeScript resolve via references. Keep paths only for truly synthetic aliases inside a single project.
Circular References and How to Break Them
Project references must form a DAG. If utils references billing and billing references utils, tsc -b will error about a circular reference. In practice, this often happens indirectly through types. For example, utils exports a generic Result<T, E> and billing uses it, but then utils tries to import a BillingError type from billing to specialize that generic. The solution is to extract shared types into a lower-level package — often called models or types — that both can reference. When you need expressive shared types, TypeScript Generics: Practical Examples Beyond the Basics provides patterns for keeping generics reusable without creating coupling.
Editor Integration Gotchas in VS Code
VS Code uses the TypeScript server in a way that is subtly different from tsc -b. If you open a file in apps/web and the editor shows no errors but tsc -b fails, check whether the file is actually part of any project. Files not included by any tsconfig.json are silently ignored by tsc -b but may be checked by the editor's default inferred project. Ensure every src file is covered by an include and that the root tsconfig.json has "files": [] so it does not accidentally create an implicit global program. Also, when adding a new reference, you must reload the editor window or restart the TS server; otherwise the editor's view of the graph lags behind the file system.
Project References vs. Alternatives: Choosing the Right Build Isolation Strategy
Project references are one strategy for achieving build isolation. They are not always the best one, and I have moved teams both toward and away from them depending on constraints. If your monorepo already uses a build system like Turborepo, Nx, or Bazel, you may be duplicating orchestration. Those tools also understand dependency graphs and cache at the task level, often with remote caching that tsc -b does not have.
The distinction is that Turborepo/Nx cache at the package script level (e.g., build), while tsc -b caches at the compiler's internal program level. You can use both: let tsc -b handle type checking with incremental builds, and let Turborepo handle bundling, testing, and remote caching of artifacts. Where teams go wrong is using path aliases plus Turborepo for build caching but skipping project references, which means type checking still runs as a single program and cannot be incrementally cached.
| Strategy | Build Isolation | Incremental Type Checking | Boundary Enforcement | Best For |
|---|---|---|---|---|
| Single tsconfig + Path Aliases | None — one program | Poor — no per-project cache | Weak — lint rules only | Small monorepos, prototypes, <3 packages |
| Project References + tsc --build | Strong — composite per project | Excellent — .tsbuildinfo per project | Strong — compiler-enforced references | Mid to large TypeScript monorepos without remote caching |
| Turborepo / Nx without References | Task-level — caches build outputs | Moderate — type check still monolithic unless split | Config-dependent — needs explicit boundaries | Monorepos needing remote cache and polyglot tasks |
| Project References + Turborepo | Strong — both compiler and task levels | Excellent — combined graph-aware caching | Strong — compiler + tooling | Large monorepos where both type and build performance matter |
If you adopt project references, expect a migration cost. You will add tsconfig.json files to every package, fix import paths that previously relied on loose resolution, and add declaration emits. Build scripts change from tsc --noEmit to tsc -b, and CI caching must be reconfigured. The payoff is not just faster builds but a compiler that understands your architecture. In a codebase with clear domain boundaries — and types modeled with tools like discriminated unions and well-constrained generics — that understanding compounds over time. New engineers cannot accidentally entangle layers because the graph makes the intended directionality explicit, and refactors that touch a single domain do not pay a global type-checking tax.
Frequently Asked Questions
Do project references require me to emit JavaScript and declaration files for every package, even apps?
Yes, for any project with composite: true, declaration emit is mandatory because downstream projects consume your .d.ts files, not your source. For apps that are never referenced by other projects, you technically could disable emit and use project references only for type checking, but I recommend keeping emit enabled uniformly. It keeps tsc -b behavior consistent, lets you use declarationMap for go-to-definition, and avoids special-casing apps in your build pipeline. If you truly do not want to emit, consider using noEmit: true with emitDeclarationOnly in a separate check config, but that adds complexity.
How do I migrate a large monorepo from path aliases to project references without a big bang?
Migrate incrementally by starting at the leaves of your dependency graph. Pick a low-level package like utils that has no internal dependencies, make it composite with declarations, and add a reference from one consumer. Verify that tsc -b and the editor still work for that subgraph, then repeat for the next layer. Keep existing paths during the migration to avoid breaking imports, but point new references at package outputs and remove path entries as you go. Use tsc -b --verbose to confirm which projects are built and tsc -b --clean to recover from stale state. In my experience, a 10-package repo can be migrated in a week of focused effort if you go layer by layer rather than trying to convert everything at once.
Why does my editor show no errors but tsc -b fails with file not in program?
This usually means the file is not included by any composite project in the reference graph. The VS Code TypeScript server can create an inferred project for files not covered by a tsconfig.json, so you see some diagnostics, but tsc -b strictly checks only files that are part of a referenced project. Check your root tsconfig.json — it should have files: [] and only references. Then verify that each package's include covers its source files and that the consumer actually references the producer. Also ensure you have reloaded the editor after changing references, since the TS server caches the graph.
Can I use project references with bundlers like Vite, esbuild, or Next.js?
Yes, but understand the separation of concerns. Bundlers resolve source files directly and do not use .d.ts for type checking. Project references primarily affect tsc -b and editor type checking, not bundling. Your bundler will still transpile from source, often via its own watcher. The benefit is that type checking becomes incremental and boundary-aware, even if bundling is separate. For Next.js, keep project references for type checking and let Next.js build as before. If you want a single tool to orchestrate both, pair tsc -b for types with Turborepo or Nx for bundler tasks, so each system does what it is best at.