Sub topic 1
Why one tsconfig stops scaling
A single tsconfig.json is the right starting point for almost every TypeScript project. It stops being the right tool when a repository grows into several packages with different needs. This chapter explains the specific costs of staying with one config, what project references change, and when you should not bother with them. Later chapters cover the configuration in detail.
Prerequisites
This chapter assumes you already know the following:
- Basic TypeScript syntax and types.
- What
tsconfig.jsondoes and what common options such astarget,strict,outDir, andincludemean. - How to run
tscfrom the command line. - How ES modules and npm package layouts work, including
importpaths and apackages/folder in a repository.
You do not need any prior knowledge of composite, references, or tsc --build. Those are introduced here and explained fully in later chapters.
The setup that works at first
Suppose a repository has two packages: core, a small library, and app, which uses it. The simplest configuration is one tsconfig.json at the root that covers everything.
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"strict": true,
"skipLibCheck": true,
"outDir": "dist"
},
"include": ["packages/*/src", "packages/*/test"]
}Running tsc here parses every matched file, builds one program, and type-checks it. A program is the compiler's in-memory model of all the files, their imports, and the types that flow between them. For a few hundred files this is fast and simple. Every limit below comes from the fact that there is only one program.
Where one config breaks down
Every run starts from the whole codebase
The compiler cannot treat core and app as separate units, because from its point of view they are one set of files. Editing a comment in core means tsc still loads and parses all files in the program before it can decide what to check again. The incremental option stores results between runs and helps a lot. Even so, the compiler still has to build the whole program each time, and the cost grows with the total file count, not with the size of your change.
You can measure this on your own repository:
tsc --noEmit --extendedDiagnosticsThe output reports the number of files, lines, memory used, and time spent in parsing, binding, and checking. If the check time is already a large share of your feedback loop, the single program is a likely cause.
Nothing enforces boundaries between packages
In one program, any file can import any other file. Nothing stops app from reaching into core/src/internal/cache.ts, a file that core never meant to expose. The compiler accepts it, because all the files are equally visible. Over time these imports accumulate, and moving or changing a file in core starts breaking code you did not know depended on it.
A project boundary changes this. Code in one project sees another project only through the declaration files (.d.ts) that it emits. That makes the other project's public surface the only thing you can depend on.
One set of options has to fit every kind of code
A single config means a single set of compilerOptions. Real repositories contain code with different requirements:
- A browser app needs
"lib": ["DOM"], while a Node library should not see DOM globals. - Test files need types such as
@types/jestor@types/node, and those globals leak into production code if they share a config. - Build scripts and tooling configs may target a different module format.
With one program you either pick the loosest options for everything or add // @ts-ignore comments. Type definitions loaded for one kind of code are visible to all of it, so mistakes such as using describe or document in the wrong place go unreported.
Output layout gets tangled
When you set outDir on a single config, the compiler mirrors the folder structure of the input files under it. The shape of that mirror depends on the common root of all the included files. Adding a test folder or a scripts folder can change that root and silently move every output path. Per-package outputs, where each package has its own dist, are hard to get from one config at all.
Editors load everything
Editors use a language service, which holds the program in memory and re-checks as you type. With one config the service for any file loads the whole repository's program. Memory use and the delay before errors appear both grow with the repository, even when you are working in a package that depends on almost nothing.
What project references change
A project reference is an entry in a tsconfig.json that says "this project depends on that other project." Each project has its own tsconfig.json, its own include, and its own options. When app references core, the compiler does not re-parse core's source files. It builds core first and reads the .d.ts files that core emitted.
| Concern | One tsconfig | Project references |
|---|---|---|
| Program size | All files in one program | One small program per project |
| Cost of a change | Rebuild the whole program, reuse cached results | Rebuild only the changed project and projects that depend on it |
| Package boundaries | Any file can import any file | Dependents see only emitted declarations |
| Compiler options | One set for all code | Each project can override the shared base |
| Output location | Depends on the common root of all inputs | Each project has its own `outDir` |
| Build order | Not applicable | Computed from the reference graph |
The right side of the diagram shows the trade you are making. Each project is smaller and isolated, but the projects must be built in dependency order, and they communicate through generated files instead of source files.
A preview of the shape
You do not need to understand every line yet. This is what the app project's config looks like in a references setup.
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"composite": true,
"rootDir": "src",
"outDir": "dist"
},
"include": ["src"],
"references": [{ "path": "../core" }]
}Three parts are new compared with the single config:
extendspulls shared options from a base file, so each project does not repeat them.composite: truemarks the project as one that other projects can depend on or build incrementally. It adds rules, such as requiring every source file to be matched byincludeorfiles.referenceslists the projects this one depends on.
The compiler builds a list of projects like this with tsc --build, which orders them using the references fields.
The costs of references
References are not free, and a good decision weighs these costs.
- More config files. Every project needs its own
tsconfig.json, plus usually a base file and a root file. - Generated declarations are required. A referenced project must emit
.d.tsfiles. Code that relies on being compiled from source, for example by a bundler that does its own type stripping, still works, but you must keeptscbuilds in the loop to get type checking. - Stale outputs. If you edit
coreand run plaintsc -p packages/app, you seecore's old declarations until you rebuildcore. Build mode (tsc --build) exists to prevent this. - No import cycles between projects. If
coreneedsappandappneedscore, you must restructure the code. This is a design constraint, and it is often a useful one. - Setup effort for small repositories. A repository with one package and a few dozen files gains little from this and pays the full configuration cost.
Signs that you have outgrown one config
Consider splitting into projects when you see several of these:
- Type checking in the editor or in CI takes noticeably longer as the repository grows, and
--extendedDiagnosticsconfirms most of the time is in checking. - You maintain several packages in one repository, and one depends on another.
- Test globals or DOM types leak into code where they do not belong.
- You want each package to produce its own
distfolder. - You want CI to rebuild only what a change affects.
If none of these apply, a single config is fine, and keeping it simple is the better choice.
Summary of the idea
A single tsconfig.json means a single program: one pool of files, one set of options, one output layout. That is ideal for a small codebase and a poor fit for a multi-package repository. Project references split the repository into several programs. Each one has its own options and its own output. They are connected by declaration files and built in dependency order. The next chapters build this up step by step, starting with a shared base config.
Exercise
A repository has packages/core and packages/app in one root tsconfig.json with include: ["packages/*/src"]. A developer changes one function body in core. Name two reasons this setup still does more work or gives weaker guarantees than a references setup.
Show answer
First, the compiler still builds one program containing every file from both packages, so the cost depends on the total repository size and not on the size of the change. Second, nothing enforces a boundary, so app can import any file in core, including internal ones, and the compiler accepts it. With references, app would see only the declarations core emits, and only core and its dependents would be rebuilt.
Exercise
You run tsc --noEmit --extendedDiagnostics in a repository with 60 source files in a single package. Check time is 0.8 seconds. Should you adopt project references? Explain your decision.
Show answer
Probably not. The program is small and the check is already fast, so references would add configuration files and build-order concerns without a meaningful gain. A better first step is to keep the single config and enable incremental if rebuilds ever become slow. Revisit references if the repository grows into multiple packages, if test or DOM types start leaking into the wrong code, or if check time becomes a large part of your feedback loop.