Sub topic 6
Splitting a Codebase with Composite Projects and References
This chapter assumes basic TypeScript types, command-line use of tsc, and familiarity with tsconfig.json and .d.ts files. A monorepo layout is helpful but not required.
Why split a TypeScript project?
A large project makes the compiler process many files together. A change in one part of the codebase can cause TypeScript to check unrelated code again.
A composite project is a TypeScript project that can be built as an independent unit. It produces build information and declaration files that another project can consume. A project reference connects one composite project to another.
For example, an application can reference a core library:
| Project | Contains | Consumer uses |
|---|---|---|
| `core` | Shared implementation | `core/dist/index.d.ts` |
| `app` | Application code | The declarations from `core` |
The application checks the library’s public types instead of checking the library’s source files every time.
Configure a composite library
Suppose the repository has this structure:
packages/
core/
src/
tsconfig.json
app/
src/
tsconfig.jsonThe library needs composite, declaration output, and separate output directories.
{
"compilerOptions": {
"composite": true,
"declaration": true,
"isolatedDeclarations": true,
"rootDir": "src",
"outDir": "dist"
},
"include": ["src"]
}The composite option enables the rules needed for project references and incremental building. It also requires TypeScript to know exactly which files belong to the project. The rootDir and include settings make that boundary clear.
The declaration option emits .d.ts files. These files describe the library’s public API without containing its runtime implementation. The consuming project can use them as a type boundary.
isolatedDeclarations is optional for project references, but it is useful when declaration generation needs to happen file by file. It requires exported declarations to contain enough explicit type information.
Reference the library from the application
The application’s configuration names the library project:
{
"compilerOptions": {
"composite": true,
"declaration": true,
"rootDir": "src",
"outDir": "dist"
},
"references": [{ "path": "../core" }],
"include": ["src"]
}The referenced path points to the directory containing the library’s tsconfig.json. In a workspace, the application normally imports the library through its package name, such as @acme/core. The workspace package configuration should expose the library’s built output.
A reference does not merge the two projects into one. It creates a dependency edge. TypeScript can build core first, then use its generated declarations while building app.
Build the project graph
Use build mode rather than invoking tsc separately for every package:
tsc --build packages/app --verboseBuild mode follows the reference graph. It builds core before app, then records what it built. On a later run, it checks timestamps and build information to decide whether each project is up to date.
You can also create a solution configuration at the repository root:
{
"files": [],
"references": [
{ "path": "./packages/core" },
{ "path": "./packages/app" }
]
}Then build everything with:
tsc --build tsconfig.json --verboseThe --verbose option explains why a project is being rebuilt or skipped. If a referenced project’s source or configuration changes, its dependents may need rebuilding too.
Common errors and trade-offs
A referenced project must produce the outputs its consumers need. Missing declarations, incorrect rootDir settings, or imports that bypass the package boundary can cause build errors.
Project references add configuration and require a clear package layout. They work best when projects have stable public APIs. If every package imports another package’s internal source files, the compiler cannot gain much from the separation.
Use references when a codebase has natural units such as libraries, services, or applications. Keep a small project together when splitting it would add more configuration than build-time savings.
Exercise
What does the references field in the application configuration tell tsc --build?
Show answer
It tells build mode that the application depends on another TypeScript project, so it should build that project first and consume its generated outputs.