Sub topic 13
Sharing settings with extends
This chapter assumes you can read a basic tsconfig.json, know what strict turns on, and have run tsc in a terminal. You will learn how one config file inherits from another, which parts are merged and which are replaced, and which mistakes cause surprises in multi-package repositories.
Why share configuration
A repository with several packages usually wants the same compiler rules everywhere: the same target, the same strict flags, the same extra checks. Copying those options into every tsconfig.json lets them drift apart. One package ends up with strict off and nobody notices.
The extends option solves this. A config names another config file, and TypeScript loads that file first. The child's own settings are then applied on top.
A base file and a child file
Put the shared settings in a base file at the repository root. The name tsconfig.base.json is a convention. It is not special to the compiler, and tsc never picks it up on its own.
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"strict": true,
"noUncheckedIndexedAccess": true,
"noImplicitOverride": true,
"skipLibCheck": true
}
}The base turns on strict, plus two checks that strict does not include: noUncheckedIndexedAccess and noImplicitOverride.
A package then inherits from it and adds what is specific to that package.
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
},
"include": ["src"]
}Save this as packages/api/tsconfig.json. When you run npx tsc inside packages/api, the compiler reads the child, follows extends to the base, and combines the two.
The diagram below shows the repository layout and the direction of inheritance.
How merging works
Inheritance follows a small set of rules. Knowing them prevents most confusion.
| Part of the config | Behavior when a child extends a base |
|---|---|
| `compilerOptions` | Merged option by option. The child's value wins when both set the same option. |
| `include`, `exclude`, `files` | Replaced as a whole. If the child sets one of them, it overrides the base's value for that key. |
| `references` | Never inherited. Each config lists its own. |
| `extends` | Can itself appear in the base, so inheritance can chain. |
The merge of compilerOptions is shallow per option. If the base sets "lib": ["ES2022"] and the child sets "lib": ["DOM"], the child's array replaces the base's array. Arrays are not combined.
The same replacement applies to include. If a child omits include, it inherits the base's list. If it sets include, the base's list is dropped entirely.
Paths are relative to the file that wrote them
Path-valued options are resolved relative to the config file where they were written, not relative to the config that ends up using them. This applies to outDir, rootDir, include, exclude, files, and baseUrl.
This matters for two reasons.
- A base file that sets
"outDir": "dist"points atdistnext to the base file, at the repository root. Every package would then write into the same folder. - A child that sets
"outDir": "dist"points atdistnext to the child. That is usually what you want.
The rule of thumb: keep path options out of the base unless they truly belong to the base's location. Put outDir, rootDir, and include in each package config, as the example above does.
The extends path itself is also relative to the file that contains it. A path starting with ./ or ../ is a file path. The .json extension may be left off.
Extending from a package
extends also accepts a package name. TypeScript resolves it using Node-style lookup in node_modules. The community publishes ready-made bases for this, such as @tsconfig/node20 and @tsconfig/strictest.
npm install --save-dev @tsconfig/node20{
"extends": "@tsconfig/node20/tsconfig.json",
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
},
"include": ["src"]
}The published base sets target, lib, and module to match Node 20. You still add the paths yourself. Since TypeScript 5.0, extends can also take an array of paths. Later entries override earlier ones, and the file's own settings override all of them.
{
"extends": ["@tsconfig/node20/tsconfig.json", "../../tsconfig.strict.json"],
"include": ["src"]
}Overriding a single flag
A child can turn off one option that the base enabled. This is the tool used during a migration, covered in a later chapter.
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"noUncheckedIndexedAccess": false,
"outDir": "dist",
"rootDir": "src"
},
"include": ["src"]
}The same works for flags inside strict. Setting "strict": true in the base and "noImplicitAny": false in the child keeps every other strict check on. The explicit individual flag always beats the umbrella, whichever file sets which.
Verifying the result
Merged configs are hard to read in your head. Ask the compiler.
npx tsc --showConfig
npx tsc --listFiles--showConfig prints the effective configuration after all extends are resolved, with paths made explicit. --listFiles prints every file in the compilation. Use the first to confirm that options came through. Use the second to confirm that include selected what you expected.
Common errors
- Error TS5083, cannot read file. The
extendspath is wrong. Check it relative to the child file, not relative to where you ran the command. - Files from the wrong folder compile. The base set
include, the child did not, and the base's relative patterns resolved against the base's folder. Setincludein each child. - Output lands in the wrong place.
outDirwas set in the base. Move it to the child. - A circular reference error. Two configs extend each other, directly or through a chain. Break the cycle.
- A flag seems ignored. A later
extendsentry or the child itself sets the same option. Run--showConfigto find the final value.
Design guidance
Keep the base small and focused on compiler behavior: language level, module settings, and strictness. Leave file selection and output paths to each package. If packages need different runtimes, such as Node and browser, create more than one base, for example tsconfig.node.json and tsconfig.browser.json, and let each package pick one. A base that tries to serve every case ends up full of overrides.
Exercise
A base file at the repository root sets "include": ["src"] and "outDir": "dist". A package at packages/api/tsconfig.json extends it and sets only "rootDir": "src". Which src folder does the compiler read, where does output go, and what change fixes the setup for the package?
Show answer
Because path values resolve relative to the file that wrote them, the package inherits include and outDir from the root base. The compiler reads the root-level src folder, not packages/api/src, and writes output to the root-level dist. The fix is to set "include": ["src"] and "outDir": "dist" in packages/api/tsconfig.json, so both resolve next to the package, and to remove them from the base.
Exercise
The base sets "lib": ["ES2022", "DOM"]. A child sets "lib": ["ES2022"]. What is the effective lib, and which command confirms it?
Show answer
The effective lib is ["ES2022"]. Arrays are replaced, not merged, and the child's value wins. Running npx tsc --showConfig prints the resolved configuration, including the final lib.