Sub topic 9
Building TypeScript Projects with `tsc --build`
Assume you know basic TypeScript, tsconfig.json, and the composite and references options. This chapter focuses on the build command that coordinates referenced projects.
What build mode does
A normal tsc invocation type-checks and emits one project. Build mode, written as tsc --build, treats your configuration files as a dependency graph. It finds referenced projects, builds dependencies first, and skips projects that are already up to date.
This matters in a repository with several packages. Without build mode, you must remember the correct order and rebuild more code than necessary. Build mode uses each project's emitted declarations and build information to make that process consistent.
A referenced project normally needs "composite": true. Its dependent project lists it in "references":
{
"files": [],
"references": [
{ "path": "./packages/core" },
{ "path": "./packages/app" }
]
}This root configuration is a solution-style configuration. It does not compile source files itself. It tells build mode which projects belong to the solution.
The everyday commands
Run these commands from the repository root:
# Build projects in dependency order.
tsc --build
# Explain which projects are out of date.
tsc --build --dry --verbose
# Rebuild even projects that appear current.
tsc --build --force
# Remove outputs and build information.
tsc --build --clean
# Keep rebuilding when source files change.
tsc --build --watchYou can also name a configuration:
tsc --build packages/app/tsconfig.jsonIf app references core, build mode includes core automatically. The command does not simply process files in the order you provide. It follows the references declared in the configuration files.
The first build emits JavaScript and declaration files according to each project's options. Later builds compare the source and configuration state with recorded build information. If only app changed, core can remain untouched.
How build mode chooses work
Use --dry --verbose when you need to understand a surprising rebuild. The output identifies projects that need rebuilding and explains conditions such as changed inputs, missing outputs, or changed compiler options.
Common build problems
A referenced project without composite: true causes an error because build mode cannot treat it as a complete build unit. Add composite to that project's configuration.
A project may also fail because a source file is not matched by include or files. With composite enabled, TypeScript requires every implementation file to belong to the project. Check the include pattern and exclude unintended directories such as generated output.
If a build keeps rebuilding everything, inspect whether generated files are inside the source tree, whether configuration files change during the build, and whether build information is being deleted. Do not use --force as the normal fix. It intentionally disables up-to-date checks.
Exercise
Your root project references packages/core and packages/app, but app is rebuilt before core. What should you inspect first?
Show answer
Inspect packages/app/tsconfig.json and confirm that its references array contains { "path": "../core" }. Then check that the root configuration points to the project directories or configuration files you intended.