Sub topic 1
Why One tsconfig Stops Scaling
A single tsconfig.json works well while a TypeScript project has one application and one build output. As a repository grows, that configuration becomes responsible for unrelated concerns: library code, applications, tests, scripts, generated files, and sometimes examples. The compiler then sees one large program instead of several projects with clear boundaries.
Prerequisites
This chapter assumes basic TypeScript syntax, common tsconfig.json options such as strict and outDir, command-line use of tsc, ES modules, and a package-based repository layout.
The limits of one project
TypeScript normally treats every file matched by one configuration as part of one project. If src, tests, and several packages are included together, a change in one area can cause the compiler to analyze everything again.
This creates several problems:
- A small change can trigger a large type-check.
- One package can accidentally import another package's source files.
- Build outputs and source files can become mixed.
- Different packages cannot easily use different compiler settings.
- The configuration cannot express which package must build first.
- Editors must load a large graph even when you are working in one package.
A monorepo is a repository that contains multiple packages or applications. Each package usually has its own source files, output directory, tests, and compiler settings. The packages may still depend on one another, so the repository needs a dependency graph.
Project references add that graph to TypeScript. A project reference says, “This project depends on that other TypeScript project.” The compiler can then build the dependency first and use its generated type declarations when checking the dependent project.
What changes with project references
Without references, the compiler sees a broad collection of files:
repository
└── tsconfig.json
├── packages/core/src
├── packages/app/src
└── packages/app/testWith references, each package becomes a project with an explicit boundary:
repository
├── packages/core/tsconfig.json
└── packages/app/tsconfig.json
└── references: [{ "path": "../core" }]The boundary is useful only if the referenced project produces declarations. A declaration file is a .d.ts file that describes exported types and functions without containing their implementation. The application can type-check against that public description instead of re-checking the library's source as part of the same program.
This separation also improves ownership. The core package controls its source files and compiler options. The app consumes the core package's public output. A mistake in the app does not silently change how the core package is compiled.
The trade-off
Project references add configuration and build artifacts. Each referenced project generally needs composite: true, an output directory, and declaration output. You also need to understand which configuration owns each file.
The payoff is a smaller unit of work. TypeScript can reuse unchanged project outputs and rebuild only affected projects. This is especially valuable when a repository contains several libraries and applications.
Common mistakes include:
- Adding a reference but forgetting
composite: truein the referenced project. - Including a file that belongs to another project.
- Importing a package's source path when the intended boundary is its built package output.
- Assuming
tsc -p packages/appwill build referenced projects automatically. Use build mode,tsc --build, for dependency-aware builds.
When to split
Split a project when it has an independent build, a stable public interface, different compiler settings, or enough files that checking it slows everyday work. Do not split every directory by default. A project boundary should represent a real dependency or deployment boundary.
Exercise
Why can project references improve a large repository even though they require more configuration?
Show answer
They divide one large compiler program into projects with explicit dependencies, allowing TypeScript to reuse declarations and rebuild only affected projects.