Sub topic 11
Build Order and the No-Cycles Rule
Prerequisites: you should know basic TypeScript, tsconfig.json, the references field, and the tsc --build command. This chapter focuses on how TypeScript orders referenced projects and why project references cannot form cycles.
References create a dependency graph
A project reference creates a directed dependency. If app references core, then app depends on core. TypeScript must build core first because the app may need the declaration files that core emits.
{
"files": [],
"references": [
{ "path": "./packages/core" },
{ "path": "./packages/app" }
]
}The root configuration lists both projects, but the order in the file does not control the build order. TypeScript reads each project's own references field and calculates the dependency order.
For example:
| Project | References | Build requirement |
|---|---|---|
| `packages/app` | `../core` | Build `core` before `app` |
| `packages/core` | none | Can build first |
The resulting order is:
- Build
core. - Read core's generated declarations.
- Build
appagainst those declarations.
This ordering is a topological order. It is an order in which every dependency appears before the project that depends on it.
Visualizing the build graph
The arrow points from a dependency to its consumer in this diagram. The root solution does not contain the application source itself. It gives tsc --build a starting point for discovering the projects.
When you run the build command, TypeScript checks timestamps and .tsbuildinfo data. It skips a project when its inputs and dependencies are already up to date.
tsc --buildUse verbose dry-run output when you want to inspect the selected order without changing build outputs:
tsc --build --dry --verboseThe output explains which projects are out of date and why.
Why cycles are forbidden
A cycle exists when following references eventually leads back to the project where you started:
corereferencesapp.appreferencescore.
Neither project can be the first dependency. If TypeScript starts with core, it needs app first. If it starts with app, it needs core first. This makes a valid dependency-first build order impossible.
A longer cycle has the same problem:
ui -> services -> data -> uiThe references graph must therefore be acyclic. A project may depend on many lower-level projects, but those dependencies must not point back upward.
Fixing a cycle
First, identify the dependency that points in the wrong direction. Then move shared types or functions into a lower-level project.
For example, instead of this:
app -> core -> appextract common code:
shared <- core
shared <- appBoth core and app can reference shared, while shared references neither of them. This preserves a one-way dependency structure.
A useful layering rule is:
- Shared types and utilities belong in lower layers.
- Domain or service projects may depend on shared layers.
- User-interface or application projects may depend on domain projects.
- Lower layers should not reference higher layers.
Exercise
The api project references models, and models references api. Which project should you change first, and what kind of fix should you seek?
Show answer
Inspect the dependency that makes models depend on api. Move the shared types or logic into a third, lower-level project so both projects can reference it without referencing each other.