Overview
Project references are a feature that allows you to structure your TypeScript programs into smaller pieces. This enables:- Faster builds through incremental compilation
- Better code organization and separation of concerns
- Enforced logical boundaries between components
- Improved IDE performance
Project references are essential for large TypeScript projects and monorepos.
How Project References Work
Project references allow TypeScript to:- Build projects independently and incrementally
- Cache build outputs in
.tsbuildinfofiles - Enforce dependencies between projects
- Navigate and edit across multiple projects efficiently
Basic Setup
1. Enable Composite Mode
For a project to be referenced, it must havecomposite: true in its tsconfig.json:
tsconfig.json
When
composite is enabled, TypeScript automatically enables declaration if not explicitly set.2. Add References
In the consuming project, add references to the dependencies:tsconfig.json
3. Build with References
Use the--build flag to build projects with references:
- Finds all referenced projects
- Detects if they’re up-to-date
- Builds out-of-date projects in the correct order
- Builds the current project
Real-World Example: TypeScript Compiler
The TypeScript compiler itself uses project references extensively. Here’s how it’s structured:Root Configuration
Fromsrc/tsconfig.json:
src/tsconfig.json
Base Configuration
Fromsrc/tsconfig-base.json:
src/tsconfig-base.json
Compiler Project
Fromsrc/compiler/tsconfig.json:
src/compiler/tsconfig.json
Services Project (with Dependencies)
Fromsrc/services/tsconfig.json:
src/services/tsconfig.json
The
services project depends on compiler and jsTyping, so it references them. TypeScript ensures they’re built first.Composite Project Requirements
Whencomposite: true is enabled, TypeScript enforces several requirements:
1. Must Specify Files
The project must specify which files are included usingfiles, include, or both.
2. Must Enable Declaration Files
declaration is automatically set to true (unless explicitly disabled, which is not recommended).
3. RootDir Constraints
All files must be within therootDir (if specified) or within the directory containing tsconfig.json.
Build Mode Commands
Project references work with TypeScript’s build mode:Basic Build
Clean Build
Force Rebuild
Verbose Output
Dry Run
Watch Mode
Monorepo Structure
Project references are ideal for monorepo setups:Root Configuration
tsconfig.json
Shared Package
packages/shared/tsconfig.json
Client Package (depends on shared)
packages/client/tsconfig.json
Web App (depends on client and shared)
apps/web/tsconfig.json
Import Behavior
When using project references, TypeScript uses the.d.ts files from referenced projects instead of the source .ts files:
Prepend Option
ForoutFile builds, you can use the prepend option:
Build Information Files
With project references, TypeScript generates.tsbuildinfo files:
- Hashes of source files
- Compiler options used
- Output file locations
- Dependency information
Performance Benefits
Before Project References
With Project References
Incremental Compilation
Project references enable true incremental compilation:- File-level tracking: Only rebuild changed files
- Project-level tracking: Only rebuild changed projects
- Dependency tracking: Only rebuild dependent projects when needed
Editor Integration
Modern editors leverage project references for better performance:VS Code
- Automatically detects project references
- Provides faster IntelliSense
- Enables cross-project navigation
- Shows build status for each project
Configuration for VS Code
.vscode/settings.json
Best Practices
Use composite mode
Always set
"composite": true for projects that will be referenced.Enable declarations
Ensure
declaration is enabled (it’s automatic with composite).Organize logically
Structure projects by logical boundaries (features, layers, etc.).
Minimize references
Only reference what you actually depend on to keep builds fast.
Recommended Options
Common Patterns
Library + App
Multi-tier Architecture
Test Separation
Troubleshooting
Build Order Issues
Error: Project references may not form a circular chain
Error: Project references may not form a circular chain
Check your references for circular dependencies:Solution: Restructure to eliminate cycles or extract shared code.
Error: Cannot find module or its corresponding type declarations
Error: Cannot find module or its corresponding type declarations
Ensure:
- Referenced project has
composite: true - Referenced project has been built (
tsc --build) - Path in reference is correct
Slow incremental builds
Slow incremental builds
- Check
.tsbuildinfofiles exist - Verify
incremental: trueis set - Ensure
outDiris consistent - Try
tsc --build --forcefor a fresh rebuild
Declaration File Issues
- Missing
declaration: true - Files outside
rootDir - Incorrect
outDirconfiguration
Advanced Configuration
Disable Source File Preference
.d.ts files from references, even when source files are available.
Disable Solution Searching
Disable Referenced Project Load
src/compiler/commandLineParser.ts:1487-1509
Migration Guide
Converting to Project References
- Identify natural boundaries in your codebase
- Create separate tsconfig.json for each project
- Enable composite mode in each tsconfig.json
- Add references between dependent projects
- Test the build with
tsc --build - Update package.json scripts
Example Migration
Before:tsconfig.json
lib/tsconfig.json
src/tsconfig.json
Related Documentation
tsconfig.json
Learn about the complete tsconfig.json structure
Compiler Options Reference
Complete reference of all compiler options