How to Use TypeScript Project References
Split a large TypeScript codebase into projects that build incrementally and enforce dependency direction, so a type-check is not a full rebuild.
On this page
Introduction
A single tsconfig over a large codebase makes every type-check a full check of every file, and a change anywhere invalidates the whole result. Project references fix this by splitting the code into multiple TypeScript projects, each with its own tsconfig, that reference each other. The build system then compiles only the projects that changed and their dependents, and enforces the dependency direction so a layer cannot accidentally import from a layer above it. This article covers the composite flag, declaring references, building with the build mode, and the errors that appear when references are configured wrongly.
Why Big Projects Need References
A single project type-checks every file on every run, and the compiler has no idea which files are affected by a change, so it cannot skip work. In a repository with a hundred thousand lines, that means seconds to minutes per check and a slow editor as the language service tracks the whole graph. The scale turns a fast tool into a sluggish one.
Project references give the compiler a unit of work: a project is a set of files with a tsconfig, and a project that references another depends on its built declarations. The compiler can then compile projects independently, reuse the output of unchanged ones, and detect a cycle where two projects reference each other. This is the structure the build mode was designed for.
The Composite Flag
A referenced project must set composite true, which tells the compiler to emit declaration files and a build info file so the project can be consumed and its freshness checked. It also enables incremental output. Without composite, a project cannot be referenced and build mode cannot skip it when it is unchanged.
// packages/lib/tsconfig.json — a referenced library project
{
"compilerOptions": {
"composite": true, // required to be referenced
"declaration": true, // emit .d.ts for consumers
"declarationMap": true, // go-to-definition into source
"rootDir": "src",
"outDir": "dist",
"module": "esnext",
"moduleResolution": "bundler",
"strict": true
},
"include": ["src"]
}
Declaring References
A project that uses another declares it in its references list, pointing at the other project’s tsconfig. The compiler then knows to build the referenced project first and to resolve imports of the referenced package to its declarations. The reference direction is the dependency direction, and it is enforced: importing a project that does not reference the current one fails.
// packages/app/tsconfig.json — depends on lib
{
"compilerOptions": {
"composite": true,
"outDir": "dist",
"paths": { "@acme/lib": ["../lib/src/index.ts"] }
},
"references": [
{ "path": "../lib" }
],
"include": ["src"]
}
Building in Order with Build Mode
The tsc build flag compiles the project and its referenced projects in the correct order, skipping the ones that are up to date by comparing inputs against the build info. Watch mode recompiles on change and only rebuilds affected projects. This is what makes the check fast on a large codebase: a change to one project rebuilds it and its dependents, not the whole repository.
The build mode also produces a .tsbuildinfo file per project that records the state; deleting it forces a rebuild, and a corrupted or stale one can cause a wrong skip. Keep it out of version control and out of the published package, and configure its location so it does not clutter the output directory.
# Build the project and its references in dependency order
tsc --build packages/app
# Watch all projects and rebuild only what a change affects
tsc --build packages/app --watch
# Force a full rebuild when the incremental state is suspect
tsc --build packages/app --force
# Where the incremental state lives (keep it out of git and packages)
# tsconfig: "tsBuildInfoFile": "dist/.tsbuildinfo"
Path Mapping and Editor Support
References resolve a package’s types to its declarations, but the editor and the bundler also need to resolve the import string to a file. Use paths mapping so an import of the package name resolves to the source or the built declaration, and keep the mapping consistent between the project that consumes and the bundler that bundles. A mismatch makes the editor show an error the compiler does not, or the bundler resolve a different file than the types.
For a monorepo, the workspace linking and the TypeScript references should agree: the package name in the workspace and the path in the tsconfig reference the same directory. When they disagree, one tool sees the source and another sees a published copy, which produces errors that are hard to trace. Align them on a single convention.
Common Pitfalls
A circular reference between two projects is rejected, which is a feature: it forces you to break the cycle by moving shared code into a third project. Without references, the cycle would compile but produce a tangled dependency graph; with references, the compiler refuses and makes the structure explicit.
Forgetting composite on a referenced project gives an error that names the flag, and omitting declaration output means consumers cannot type-check against the project. A stale build info file makes the compiler skip a project that actually changed, so if a change seems ignored, delete the tsbuildinfo and rebuild. Finally, exclude test files from the composite project unless the tests are part of the package, so the published declarations do not include test types.
Conclusion
Project references turn a monolithic TypeScript codebase into a graph of projects that build incrementally and enforce dependency direction. Set composite and declaration output on each referenced project, declare references in the consumers, build with the build mode so only changed projects and their dependents recompile, and align path mapping with the workspace naming. A circular reference is a configuration error worth fixing structurally, and a stale build info is the usual cause of a change that seems ignored. Then the type-check stays fast as the codebase grows.
Sources: TypeScript: Project References, TypeScript: build mode, TypeScript: composite.
FAQ
- What does composite do on a referenced project?
- It makes the project emit declarations and a build info file, so it can be consumed by another project and its freshness can be checked. It is also what enables incremental builds and lets build mode skip unchanged projects.
- Why does tsc --build sometimes skip a project I changed?
- Usually a stale or corrupted build info file, which makes the compiler think the project is up to date. Delete the tsbuildinfo file and rebuild; if the change is then picked up, the incremental state was wrong.
Report a problem with this articleOpens a pre-filled issue on GitHub.