patterntypescriptnoneModerate
TypeScript Project References for Monorepo Builds
Viewed 0 times
TypeScript 3.0+
project referencescompositemonorepoincremental buildtsc --build
Error Messages
Problem
In a monorepo, building one package rebuilds all its dependencies from scratch. Type checking is slow and packages cannot reference each other's type information without full recompilation.
Solution
Use TypeScript Project References to enable incremental cross-package compilation.
// packages/core/tsconfig.json
{
"compilerOptions": {
"composite": true,
"declaration": true,
"declarationMap": true,
"outDir": "./dist"
}
}
// packages/app/tsconfig.json
{
"compilerOptions": { "outDir": "./dist" },
"references": [
{ "path": "../core" }
]
}
// Root tsconfig.json
{
"references": [
{ "path": "packages/core" },
{ "path": "packages/app" }
],
"files": []
}# Build all with dependency order
tsc --build
# Build only changed packages
tsc --build --incrementalWhy
Project references allow tsc to cache .d.ts outputs of referenced projects. When only one package changes, tsc rebuilds only that package and its dependents, not the entire tree.
Gotchas
- Each referenced project must have 'composite: true' and 'declaration: true' enabled.
- Project references only work with 'tsc --build' (or '-b'), not plain 'tsc'.
- Circular references are not allowed and cause a build error.
Revisions (0)
No revisions yet.