Sub topic 3
A Solution-Style Root `tsconfig`
Assume you know basic TypeScript, tsconfig.json, ES modules, and the tsc command. This chapter shows how a repository-level configuration coordinates several referenced projects.
A Solution-Style Root `tsconfig`
A solution-style configuration compiles no files itself. Its files array is empty, and its references array lists the projects that make up the repository.
{
"files": [],
"references": [
{ "path": "./packages/core" },
{ "path": "./packages/app" }
]
}Run tsc --build from the repository root. TypeScript reads each referenced project and builds the dependency graph.
Build Order and the No-Cycles Rule
If app/tsconfig.json contains { "path": "../core" }, TypeScript builds core before app. The referenced project must be a composite project, so it produces declarations and build metadata.
References form a directed acyclic graph. A cycle such as core referencing app while app references core is invalid. Move shared code into a third project, or remove one dependency.
Go to Definition Across Projects
Editors use project references and declaration maps to understand boundaries. When app imports a symbol from core, Go to Definition can open the original source file when core was built with declarationMap: true.
Without current outputs, editor results can be stale. Build the referenced project after changing its public API.
Separate Projects for Tests and Tooling
Tests and build scripts can have their own configurations. For example, packages/core-test/tsconfig.json can reference packages/core without adding test files to the production project. A tooling project can separately include scripts that run in Node.js.
This separation keeps production output, test output, and tooling dependencies distinct.
Project References in CI
CI should install dependencies, build from the root, and fail on compiler errors. Use a clean workspace for reproducible results. Caching .tsbuildinfo can speed repeated builds, but the cache must match the source revision and compiler version.
| Command | Use |
|---|---|
| `tsc --build` | Build changed projects in dependency order |
| `tsc --build --clean` | Remove referenced project outputs |
| `tsc --build --force` | Rebuild every project |
Troubleshooting Common Errors
- “Referenced project must have
compositeenabled”: add"composite": trueto the referenced configuration. - “File is not listed”: ensure every source file matches
includeor appears infiles. - Stale declarations: run
tsc --build --force. - A dependency is rebuilt unexpectedly: inspect
outDir,.tsbuildinfo, and the project’s reference paths.
Exercise
Which project should a root solution tsconfig.json compile directly?
Show answer
None. Set "files": [] and list the actual projects in "references".