Sub topic 10
A Solution-Style Root tsconfig
This chapter assumes you know basic TypeScript syntax, common tsconfig.json options, ES modules, and how to run tsc. It also assumes that each package already has its own project configuration with "composite": true.
A solution-style root configuration is a tsconfig.json that coordinates several TypeScript projects. It usually contains no source files of its own. Instead, its references field lists the projects that make up the repository.
Create the root configuration
Imagine this repository:
repository/
├── tsconfig.json
└── packages/
├── core/
│ ├── src/
│ └── tsconfig.json
└── app/
├── src/
└── tsconfig.jsonThe root tsconfig.json can list both package projects:
{
"files": [],
"references": [
{ "path": "./packages/core" },
{ "path": "./packages/app" }
]
}The empty "files" array is intentional. It tells TypeScript that the root project has no files to compile. The root file acts as an entry point for build mode.
The path values point to project directories or directly to their configuration files. When a directory is used, TypeScript looks for tsconfig.json in that directory.
The package configurations still declare their own source files, output folders, and dependencies. For example, packages/app/tsconfig.json can reference core:
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"composite": true,
"rootDir": "src",
"outDir": "dist"
},
"include": ["src"],
"references": [
{ "path": "../core" }
]
}Build the complete repository
Run build mode from the repository root:
tsc --buildBecause the command starts with the root configuration, TypeScript reads the project graph. It sees that app depends on core, so it builds core first. It then builds app using core’s declaration output.
flowchart TD
Root["Root tsconfig.json"]
Core["packages/core"]
App["packages/app"]
Core --> App
Root --> Core
Root --> AppThe root file lists both projects, while the app reference establishes the dependency edge. Listing a project in the root file does not, by itself, define a dependency between two packages.
Useful build commands
# Build projects that are missing or out of date
tsc --build
# Explain which projects would be built
tsc --build --dry --verbose
# Rebuild every project
tsc --build --force
# Remove generated output and build information
tsc --build --clean
# Keep rebuilding after source changes
tsc --build --watchBuild mode uses each project’s .tsbuildinfo file to compare the current inputs with the previous build. An unchanged project can be skipped. This is useful both locally and in continuous integration, although CI should use a controlled cache if it wants to preserve these files between runs.
> callout note > The root configuration coordinates projects. It does not replace the package configurations. Put compiler options such as strict, target, and module in a shared base configuration, then put source boundaries and project references in the individual package files.
Keep the project graph valid
Every project listed in references must be a valid composite project. Its configuration must normally include "composite": true, and every source file must match include or files.
References must form a directed acyclic graph. If core references app while app already references core, TypeScript cannot choose a valid build order. Move shared code into a third project or remove one of the dependencies.
A solution-style root also gives CI one stable command:
tsc --build tsconfig.jsonThis command builds the same graph locally and in automation, instead of requiring CI to remember each package and its correct order.
Exercise
Why does a solution-style root tsconfig usually set "files" to []?
Show answer
The root coordinates referenced projects but has no source files of its own, so an empty "files" array prevents it from compiling repository code as a separate project.