Sub topic 6
Declaring dependencies with references
This chapter assumes you know what a tsconfig.json does and can run tsc. It also assumes you have read the earlier chapters on extends, composite, and declaration output. Here you will connect projects with the references field and see what the compiler does with it.
What references mean
A project reference says: "this project depends on that project, and I will consume its built output instead of its source." The references field is a top-level array in tsconfig.json. Each entry is an object with a path property.
Here is the app project from the running example:
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"composite": true,
"rootDir": "src",
"outDir": "dist"
},
"include": ["src"],
"references": [{ "path": "../core" }]
}The reference does three things:
- It tells
tsc --buildto buildpackages/corebeforepackages/app. - It tells the type checker to read
core's emitted.d.tsfiles whenappimports from it. The compiler does not re-checkcore's source. - It lets the editor and the compiler know that
appmay import fromcore, and thatcoreis a separate unit with its own options.
A reference does not add core's files to app's program. It also does not install anything or change runtime module resolution. It only affects type checking and build order.
The path value
The path can point to a directory that contains a tsconfig.json, or to a specific config file:
{
"references": [
{ "path": "../core" },
{ "path": "../tooling/tsconfig.build.json" }
]
}Paths are resolved relative to the config file that declares them. Use the file form when a directory holds several configs, for example tsconfig.json for the editor and tsconfig.build.json for production output.
Requirements on the referenced project
The compiler enforces rules on any project that appears in a references list. Most of them follow from the need for reliable .d.ts output.
- The referenced project must set
"composite": true. This also turns ondeclarationandincremental. - Every source file must be matched by
includeor listed infiles. Withoutcomposite, files outside the list are quietly pulled in when imported. With it, the compiler stops with an error. - The referenced project must emit. You cannot set
noEmiton it, because the referencing project needs the declaration files. - The referenced project must have been built. If
core/distdoes not exist,apphas no declarations to read.
Seeing it work
Core exports one function:
export function greet(name: string): string {
return `Hello, ${name}!`;
}The app imports it by relative path:
import { greet } from "../../core/src/greet.js";
console.log(greet("project references"));The import names a .ts source location, since greet.js maps to greet.ts. When the compiler resolves it, it notices that the file belongs to a referenced project. It then swaps in the matching output file, packages/core/dist/greet.d.ts, computed from core's rootDir and outDir. This is why the build order matters, and why rootDir should be set explicitly. If rootDir were inferred differently, the computed output path might not match the real one.
Because app has rootDir: "src", you might expect an import from ../../core/src to fail with a "file is not under rootDir" error. It does not, because the file is never added to app's program as a source file. Only its declaration output is read.
The dependency graph
With more than two projects, the references form a directed graph. Each arrow points from the project that depends to the project it depends on.
tsc --build walks this graph from the dependencies upward. In the graph above it builds utils, then core, then app, then tests. Projects that do not depend on each other have no ordering between them.
You list only direct dependencies. app references core and utils because it imports both. If app imported only core, it would not need to list utils, since core's declarations already carry the type information they need. Still, if app imports a type that core re-exports from utils, the declaration files in core/dist point at utils, so utils must be built as well. tsc --build handles that because it builds the whole graph.
Rules for the graph
- No cycles. If
corereferencesappandappreferencescore, the compiler reportsTS6202: Project references may not form a circular graph. Break the cycle by moving the shared code into a third project that both reference. - One-directional types. Because a project reads declarations, a change in
core's public types can breakapp. A change that only touchescore's internals, with identical declarations, will not requireappto be re-checked. - References are not inherited.
extendscopiescompilerOptions,include,files, andexcludefrom the base. It does not copyreferences. Each project declares its own, which is why the base config in the earlier chapter has none.
Plain tsc versus build mode
Running tsc -p packages/app on a project with references does not build core. It type-checks app only, and it expects core/dist to be present already. If it is missing, you get an error. Use build mode to get the ordering and rebuild behavior:
tsc --build packages/appThis builds core first if its outputs are out of date, then builds app. The next chapter covers build mode in detail.
Common errors with references
| Code | Message (shortened) | Usual cause |
|---|---|---|
| TS6306 | Referenced project must have setting "composite": true | The referenced project's config lacks `composite`. |
| TS6307 | File is not listed within the file list of project | A file is imported but is not matched by `include` or `files`. |
| TS6305 | Output file has not been built from source file | The referenced project has not been built yet. Run `tsc --build`. |
| TS6310 | Referenced project may not disable emit | `noEmit: true` is set on a referenced project. |
| TS6202 | Project references may not form a circular graph | Two or more projects reference each other. |
TS6305 is the most common one for newcomers. It appears when someone runs tsc -p or opens the editor before any build has produced dist. Building the referenced project fixes it.
Referencing with package names
The example imports across projects with a relative path, which keeps the setup small. Many repositories import by package name instead, for example import { greet } from "@acme/core", using npm workspaces. In that case each package has a package.json whose types or exports field points at the built .d.ts files. The references entry is still needed. It is what links the package name's resolved declaration file back to the project that produces it, so the compiler can order and check the build.
Exercise
A repository has packages/a and packages/b. Project b imports from a and lists { "path": "../a" } in references. Running tsc --build packages/b fails with TS6306 for project a. Separately, after you fix that, you add { "path": "../b" } to the references in a and see TS6202. What change fixes each error, and why?
Show answer
TS6306 means project a is referenced but does not set "composite": true, so add that option (with rootDir, outDir, and a matching include) to packages/a/tsconfig.json. TS6202 means a and b now reference each other, which is a cycle. Remove the reference from a to b. If a truly needs code from b, move the shared code into a third project that both a and b reference.