Project References
Project references split a TypeScript codebase into smaller tsconfig projects that declare dependencies on each other. The compiler builds them in order and uses each project's .d.ts output as the types for its dependents.
Detailed Explanation
A single tsconfig.json that includes a whole monorepo is slow and loosely scoped. Every file can import every other file. A change in a leaf package re-checks the world. Project references are TypeScript's built-in answer: each package (or each layer) is its own project, and the dependency graph is written in JSON.
The child project sets "composite": true (and usually "declaration": true). Composite mode requires an explicit include/files list and produces .d.ts files plus a .tsbuildinfo cache. The parent, or a sibling that imports it, lists { "path": "../shared" } in "references".
When you run tsc -b (build mode) at the root, the compiler walks the reference graph, rebuilds stale projects, and skips the rest. Imports of a referenced project resolve to its declaration output, not to its source, unless you turn on disableSourceOfProjectReferenceRedirect the other way — the default in editors is to jump to source for a better go-to-definition, while tsc -b still uses the build graph.
This is how you type a monorepo without putting every package in one include. It is also how you enforce layering: app can reference ui and domain; domain does not reference app. An import that is not backed by a reference is an error under tsc -b if you also set the solution-style root correctly, and it is a smell even when it is not.
A solution tsconfig.json at the root often has empty files and a references array only. It exists so tsc -b has a single entry point. Each real project still has its own include list and its own outDir.
The costs are operational. You must emit declarations for referenced projects. You must keep references in sync with package.json dependencies — TypeScript will not read npm workspace metadata for you. A forgotten reference looks like "cannot find module" or like types from an old build. CI should run tsc -b --clean occasionally and tsc -b on every change.
Use references when you have more than one compile unit: a library plus an app, a set of workspace packages, or a server and a client that share a types package. Do not add them to a single-folder app; they add machinery without a graph to justify it.
Code Examples
1. A composite library package.
// 1. A composite library package.
{
"compilerOptions": {
"composite": true,
"declaration": true,
"declarationMap": true,
"outDir": "dist",
"rootDir": "src",
"strict": true
},
"include": ["src"]
}2. An app that depends on that library.
// 2. An app that depends on that library.
{
"compilerOptions": {
"outDir": "dist",
"rootDir": "src",
"strict": true,
"noEmit": false
},
"include": ["src"],
"references": [{ "path": "../shared" }]
}3. A solution config at the repo root — entry point for tsc -b.
// 3. A solution config at the repo root — entry point for tsc -b.
{
"files": [],
"references": [
{ "path": "./packages/shared" },
{ "path": "./packages/app" }
]
}Common Gotchas
`composite` requires `declaration` and an explicit file list
a composite project with only loose defaults will refuse to build. Set `include` or `files`.
`tsc` without `-b` does not build the graph
`tsc -p app` type-checks the app against existing `.d.ts` output and will not rebuild `shared`. Use `tsc -b` in CI.
Editors may jump to source while `tsc -b` uses `.d.ts`
if source and last emit disagree, you can see errors in one place and not the other. Rebuild the referenced project.
`references` is not inferred from package.json
adding an npm dependency does not add a TypeScript reference. Keep the two graphs aligned.