Sub topic 6
Declaring Dependencies with `references`
Assume you know basic TypeScript, tsconfig.json, ES modules, and the tsc command. This chapter focuses on the references field: how one TypeScript project declares a dependency on another and how that declaration controls build order.
What a project reference does
A project reference connects two TypeScript projects. The project that contains the reference is the dependent project. The referenced project is its dependency.
For example, an application may use a shared core package:
packages/
core/
tsconfig.json
src/greet.ts
app/
tsconfig.json
src/main.tsThe application config can declare its dependency like this:
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"composite": true,
"rootDir": "src",
"outDir": "dist"
},
"include": ["src"],
"references": [
{ "path": "../core" }
]
}The path points to the referenced project directory. TypeScript looks in that directory for a tsconfig.json file. You can also point directly to a configuration file, such as "../core/tsconfig.json".
Requirements for the referenced project
A referenced project must normally enable composite:
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"composite": true,
"declaration": true,
"rootDir": "src",
"outDir": "dist"
},
"include": ["src"]
}composite makes the project suitable for build mode. It also makes TypeScript enforce project boundaries more strictly. Every implementation file must be included by the project’s include or files setting.
The referenced project must produce declarations. A declaration file, such as greet.d.ts, describes exported types and functions without containing their implementation. The dependent project can type-check against these declarations instead of checking the dependency’s source files again.
flowchart TD
Core["packages/core<br/>composite project"] -->|declarations| App["packages/app<br/>references ../core"]
App --> Build["tsc --build"]Build order and dependency boundaries
When you run tsc --build from the application project, TypeScript first builds core if it is missing or out of date. It then builds app.
tsc --build packages/app/tsconfig.jsonThis order comes from the reference, not from the order of files in include. A reference describes a project-level dependency.
A project reference does not automatically change JavaScript module resolution. Your import must still use a path that your runtime and package setup understand:
import { greet } from "../../core/src/greet.js";
console.log(greet("project references"));In a published package, you would usually import the package name instead. The exact import path depends on your package exports and runtime configuration.
The no-cycles rule
Project references form a directed graph. If app references core, core cannot reference app. A cycle prevents TypeScript from finding a valid build order.
Keep lower-level projects independent from higher-level projects. For example, shared types and utilities can be dependencies of an application, but they should not import application code.
Check a reference before building
Use verbose dry-run output to inspect the dependency order without writing build outputs:
tsc --build packages/app/tsconfig.json --dry --verboseLook for the referenced project in the output. If TypeScript reports that the referenced project is not composite, enable composite in that project’s configuration. If it reports that a file is not listed, fix its include or files setting.
Exercise
The app project references core, but the build reports that core is not a composite project. What should you change?
Show answer
Add "composite": true to core's compilerOptions, ensure its source files are included, and configure declaration output so app can consume its public types.