Sub topic 7
Importing Across Project Boundaries
Assume you know basic TypeScript types, tsconfig.json, outDir, ES modules, and how to run tsc. This chapter shows how one referenced project consumes another without compiling the dependency as part of its own source set.
The project boundary
A project reference connects two TypeScript projects. The referenced project is the dependency. It must produce declaration files, which describe its public types without containing implementation code.
For example, a repository can contain a reusable core package and an app package:
packages/
core/
src/greet.ts
tsconfig.json
app/
src/main.ts
tsconfig.jsonThe core project exports a function:
export function greet(name: string): string {
return `Hello, ${name}!`;
}The app project can import that function:
import { greet } from "../../core/src/greet.js";
console.log(greet("project references"));The .js extension matches the emitted ES module file. TypeScript resolves the corresponding .ts source during development and uses the referenced project's declaration output when the project reference is active.
Declaration output is the contract
A referenced project normally needs composite: true. Composite projects emit declarations and follow stricter file-listing rules:
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"composite": true,
"declarationMap": true,
"rootDir": "src",
"outDir": "dist"
},
"include": ["src"]
}The compiler emits files such as these:
packages/core/dist/
greet.js
greet.d.ts
greet.d.ts.mapThe .d.ts file is the boundary contract. For greet.ts, it contains the function signature but not the function body:
export declare function greet(name: string): string;The declaration map connects that generated contract back to the source file. Editors can then provide navigation and readable diagnostics across the project boundary.
Prefer a package entry point
A relative import into src is useful for a small demonstration, but a larger repository should expose a stable package entry point. Do not make consumers depend on internal file names unless that layout is part of the intended API.
A package can expose core through its package metadata or a workspace alias. Consumers then import the package:
import { greet } from "@example/core";
console.log(greet("project references"));This keeps the dependency's src and dist layouts private. The package's types or exports settings tell TypeScript where to find the declarations.
What the reference changes
The app configuration declares that core must be built first:
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"composite": true,
"rootDir": "src",
"outDir": "dist"
},
"include": ["src"],
"references": [{ "path": "../core" }]
}When TypeScript builds app, it can consume core's emitted declarations instead of type-checking all of core's source files again. This reduces repeated work and gives each project a clear ownership boundary.
The reference does not automatically make every import valid. The imported symbol must be exported, the dependency must emit declarations, and the import path or package name must resolve correctly.
Common boundary errors
| Error | Likely cause | Fix |
|---|---|---|
| Referenced project must have `composite` enabled | The dependency is not configured as a composite project | Add `"composite": true` |
| File is not listed within the file list | A source file is outside `include` or `files` | Correct the project's file patterns |
| Cannot find module | The package path, export, or emitted declaration path is wrong | Check the import, `outDir`, and package metadata |
| Exported symbol is missing | The dependency does not export the requested name | Add an export or change the import |
Exercise
Why does a consuming project need the referenced project's declaration files?
Show answer
Declaration files provide the dependency's public types without requiring the consumer to re-check the dependency's implementation source.