Sub topic 3
Turning a Project into a Composite Project
Assume you know basic TypeScript types, tsconfig.json, outDir, ES modules, and how to run tsc. This chapter turns one package into a project that another TypeScript project can reference.
Why `composite` matters
A normal TypeScript project can type-check its source files directly. A referenced project needs a stronger boundary. The consuming project should read the producer's generated declaration files instead of re-checking the producer's source every time.
The composite compiler option marks a project as ready for project references. It also gives the compiler enough information to determine whether the project is up to date.
A composite project has important constraints:
| Requirement | Purpose |
|---|---|
| Every source file matches `files` or `include` | The compiler must know the complete project |
| Declaration output is enabled | Dependent projects need type information |
| Output paths are defined clearly | Generated files must not overlap source files |
Configure the project
Suppose the repository contains a shared core package:
packages/
core/
src/
greet.ts
tsconfig.jsonCreate packages/core/tsconfig.json:
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"strict": true,
"skipLibCheck": true,
"composite": true,
"declarationMap": true,
"rootDir": "src",
"outDir": "dist"
},
"include": ["src"]
}The include pattern is essential. With composite, TypeScript reports an error if an implementation file belongs to the project directory but is not included by files or include.
composite also enables declaration generation. The compiler emits a .d.ts file for each TypeScript module. A declaration file contains public types and function signatures, but not the implementation. declarationMap adds source maps so an editor can navigate from a declaration back to the original source.
Add a source file:
// packages/core/src/greet.ts
export function greet(name: string): string {
return `Hello, ${name}!`;
}Run the compiler from the repository root:
tsc --project packages/core/tsconfig.jsonThe output resembles this:
packages/core/dist/
greet.d.ts
greet.d.ts.map
greet.js
greet.js.map
tsconfig.tsbuildinfoThe .d.ts file is the project boundary. The .tsbuildinfo file records information about the previous build, allowing TypeScript to decide what must be rebuilt later.
Understand the constraints
A composite project is not simply a faster configuration. It changes how the compiler validates project structure.
Common mistakes include:
- Setting
"composite": truebut excluding a source file. - Writing output into
src, which mixes generated files with source files. - Expecting another project to consume TypeScript source without declaration output.
- Moving files without updating
include,rootDir, oroutDir.
Use a separate dist directory and keep the project boundary predictable. Later, a dependent project can list this project in its references array.
Exercise
Why must every implementation file be matched by files or include in a composite project?
Show answer
TypeScript must know the complete set of project inputs to validate build state and safely determine whether the project is up to date.