Sub topic 17
Project references for multi-package repositories
Assume you know basic TypeScript configuration, include, outDir, and extends. This chapter focuses on repositories that contain several TypeScript packages, where one package imports another.
A project reference connects one tsconfig.json to another. The referenced project is built first, and the dependent project can consume its generated declaration files. This gives TypeScript an explicit build graph instead of treating the repository as one large compilation.
Why project references matter
Suppose a repository contains an app package and a shared package:
packages/
shared/
src/
tsconfig.json
app/
src/
tsconfig.jsonThe app depends on shared. Its configuration can declare that relationship:
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"strict": true,
"composite": true,
"declaration": true,
"outDir": "dist",
"rootDir": "src"
},
"include": ["src"],
"references": [
{ "path": "../shared" }
]
}The path points to the referenced project directory or its configuration file. The referenced project also needs a compatible configuration:
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"strict": true,
"composite": true,
"declaration": true,
"declarationMap": true,
"outDir": "dist",
"rootDir": "src"
},
"include": ["src"]
}composite marks a project as suitable for referencing. It requires TypeScript to account for the project's files and produces a build-information file. declaration emits .d.ts files, which describe the package's public types without exposing its implementation.
Building the dependency graph
Use build mode with the -b or --build command:
npx tsc -b packages/app/tsconfig.jsonTypeScript reads the app's references, builds shared first when necessary, and then builds app. It stores incremental information in .tsbuildinfo files so later builds can skip projects that have not changed.
A common repository layout includes a solution configuration that emits no JavaScript itself:
{
"files": [],
"references": [
{ "path": "./packages/shared" },
{ "path": "./packages/app" }
]
}Run the entire graph with:
npx tsc -b tsconfig.jsonThe references form a directed graph. A project may depend on another project, but circular references are invalid because TypeScript cannot choose a build order.
Common errors and trade-offs
Each referenced package should have a clear public boundary. Importing another package's source files directly can bypass its declarations and make the graph less useful. Prefer importing through the package's public entry point.
Project references add configuration and generated files, but they reduce rebuild time and memory use in large repositories. They work best when packages have stable boundaries and each package emits declarations.
Exercise
Which project must TypeScript build first when app references shared, and which compiler option makes the referenced project's public types available?
Show answer
TypeScript builds shared first. The declaration option emits the .d.ts files that describe its public types.