Sub topic 2
A shared base config with extends
This chapter assumes you can read a tsconfig.json, know what target, strict, module, and outDir do, and can run tsc from a terminal. You do not need project references yet. This chapter prepares the ground for them.
Why a base config comes first
A repository with several referenced projects has one tsconfig.json per project. Each project needs the same language settings: the same target, the same strict flag, the same module system. If you copy those options into every file, they drift. One package ends up with strict: false and another targets an older runtime, and type errors then depend on which package you are in.
The extends field fixes this. A config names another config file, and the compiler starts from that file's options and applies the local ones on top. You write the shared options once. Each project then states only what is specific to it.
The base file
Put a tsconfig.base.json at the repository root. The name matters little, but .base.json has two advantages. The compiler does not pick the file up when you run tsc with no arguments, because it looks for tsconfig.json. And editors treat it as an ordinary shared file, not as a project of its own.
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"strict": true,
"skipLibCheck": true
}
}These options describe the language and the environment. They apply to every package. The base file has no include, files, outDir, or rootDir, and the reasons for that appear below.
Extending it from a project
A project config points to the base with a relative path:
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"composite": true,
"declarationMap": true,
"rootDir": "src",
"outDir": "dist"
},
"include": ["src"]
}This is packages/core/tsconfig.json. The effective configuration is the base options plus the four local ones. Later chapters explain composite and the declaration options. Here, only the inheritance matters.
How merging works
Knowing the merge rules prevents most surprises.
- Options merge key by key. If both files set
strict, the child's value wins. Options that only the base sets are kept. - Arrays are replaced, not combined. If the base sets
"lib": ["ES2022"]and the child sets"lib": ["DOM"], the result is["DOM"]. The child does not get both. files,include, andexcludeoverwrite. If the child defines any of them, it replaces the base's value. These three fields are top-level, not insidecompilerOptions.referencesis never inherited. It is the one top-level field thatextendsskips. Each project must list its own references.- Circular chains are an error. A extending B extending A is rejected.
You can set extends to a chain. core can extend tsconfig.node.json, which extends tsconfig.base.json. Keep chains short, because every extra level makes it harder to see where a value came from.
| Field | Where it lives | Inherited through `extends`? |
|---|---|---|
| `compilerOptions.*` | Base or project | Yes, merged per option |
| `include`, `files`, `exclude` | Project | Yes, but a child value replaces the whole field |
| `references` | Project | No |
| `extends` | Project | Not applicable. It is the mechanism |
Path options resolve against the file that wrote them
This is the most common mistake with a base config. A relative path in an inherited option is resolved relative to the config file where it was written. It is not resolved relative to the project that inherits it.
Suppose you put "outDir": "dist" in the base file. Every project inherits an outDir that points to <repo root>/dist. Both core and app then write their output into the same folder and overwrite each other's files. A shared rootDir has the same problem.
There are two ways to avoid this:
- Keep location-specific options (
rootDir,outDir,include,tsBuildInfoFile) in each project config, as shown above. - Use the
${configDir}template variable, available from TypeScript 5.5. It expands to the directory of the config file that is being compiled, not the one that defines the option.
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"strict": true,
"skipLibCheck": true,
"rootDir": "${configDir}/src",
"outDir": "${configDir}/dist"
},
"include": ["${configDir}/src"]
}With this base, every project that has a src folder gets its own dist folder with no per-project lines. The cost is that every package must follow the same layout. For most monorepos that is a reasonable rule, but a package with a different layout has to override the values.
Extending more than one file
From TypeScript 5.0, extends can be an array. The files apply in order, and later entries override earlier ones. A project can then combine an environment preset with your own base:
{
"extends": ["@tsconfig/node20/tsconfig.json", "../../tsconfig.base.json"],
"compilerOptions": {
"composite": true,
"rootDir": "src",
"outDir": "dist"
},
"include": ["src"]
}The first entry is a package from npm. A bare package name or a path that starts with a package name is looked up in node_modules, so install the preset first. Because the repository's own base comes last, its options win wherever the two files disagree. Put the file you trust most at the end.
Deciding what goes in the base
A useful rule: put an option in the base if it should be identical in every project, and keep it in the project if it describes that project's own files or its role in the build graph.
Good candidates for the base:
target,module,moduleResolutionstrictand the other strictness flagsskipLibCheck,esModuleInterop,forceConsistentCasingInFileNames
Good candidates for each project:
rootDir,outDir,include,excludereferenceslibortypes, when one package targets a browser and another targets Node
composite is a judgment call. If every project in the repository is referenced, putting composite: true in the base saves repetition. If some projects are standalone, such as a script folder, forcing composite on them adds constraints they do not need. The examples in this tutorial set it per project so that each config shows what makes it referenceable.
Verifying the result
Do not guess what a project ends up with. Ask the compiler to print the merged configuration:
tsc --showConfig -p packages/core/tsconfig.jsonThe output lists the final compilerOptions, with inherited paths converted to absolute paths, and the resolved files list. Check three things: strict is true, outDir ends in packages/core/dist, and the file list contains only files from packages/core/src. If outDir points to the repository root, you have hit the path problem described above.
Common errors
- Output files appear in the wrong folder. A relative
outDirorrootDirin the base resolved against the base file's location. Move it to the project, or use${configDir}. - A project compiles the whole repository. The base file sets
includeorfiles, or the project sets none. Withoutinclude, the compiler takes every.tsfile under the project directory. Setincludein each project. referencesseems to be ignored in a child.referencesis not inherited. Declare it in each project that needs it.- Error "File ... not found" for the
extendstarget. The path is relative to the extending file, not to the directory where you runtsc. Count the../segments from the project's own folder. - An array option lost values. Arrays replace instead of merge. Repeat the full list in the child.
Exercise
A base config at the repository root contains "outDir": "dist". Two projects, packages/core and packages/app, extend it and set no outDir of their own. Where does each project write its output, and what are two ways to fix it?
Show answer
Both projects write to <repo root>/dist, because a relative path in an inherited option resolves against the config file that defines it. Their outputs collide. Fix it by (1) removing outDir from the base and setting "outDir": "dist" in each project config, or (2) writing "outDir": "${configDir}/dist" in the base on TypeScript 5.5 or later, so each project gets its own dist folder.
Exercise
The base sets "lib": ["ES2022", "DOM"] and packages/server/tsconfig.json sets "lib": ["ES2022"]. Which libraries does server use, and which command shows you the final answer?
Show answer
Only ES2022, because an array in the child replaces the base's array instead of merging with it. Running tsc --showConfig -p packages/server/tsconfig.json prints the merged configuration.