How to use project references in TypeScript
· Category: TypeScript
Short answer
Project references let you break a TypeScript application into multiple projects that can be built independently while preserving type safety across boundaries.
Steps
- Enable
composite: truein each referenced project's tsconfig.json. - Set
declaration: trueso referenced projects produce.d.tsfiles. - In the root tsconfig.json, add a
referencesarray pointing to subproject paths. - Build the entire solution with
tsc --buildortsc -b. - Import across projects using normal module resolution.
Tips
tsc --buildcaches outputs and only rebuilds changed projects, dramatically speeding up CI.- Keep project boundaries aligned with team ownership and deployment units.
Common issues
- Circular project references are not supported and must be refactored.
- Missing
compositeordeclarationflags produce build errors.