Sub topic 13
Sharing TypeScript Settings with `extends`
This chapter assumes you know basic TypeScript syntax, JSON, and how to run tsc from a terminal. You will learn how multiple projects can share one compiler configuration while keeping package-specific settings separate.
Why share a configuration?
A tsconfig.json file controls how TypeScript checks and compiles a project. In a repository with several packages, copying the same settings into every package creates drift. One package might use strict: true, while another accidentally leaves it disabled.
The extends option lets one configuration inherit another. Put shared rules in a base file, then override only the settings that differ.
A common structure looks like this:
repository/
├── tsconfig.base.json
└── packages/
├── api/
│ ├── src/
│ └── tsconfig.json
└── web/
├── src/
└── tsconfig.jsonCreating a base configuration
The base file contains settings that should apply everywhere:
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"strict": true,
"noUncheckedIndexedAccess": true,
"noImplicitOverride": true,
"skipLibCheck": true
}
}A package configuration references that file:
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
},
"include": ["src"]
}The path in extends is resolved relative to the package configuration. Here, ../../tsconfig.base.json moves from packages/api to the repository root.
The package receives the base compiler options, then applies its own options. If both files define the same option, the child configuration wins. For example, adding "strict": false to the package file would disable strict mode for that package, so avoid overrides that weaken the repository standard unless there is a migration plan.
include, exclude, and files belong to the project that declares them. Define the file list in each package when packages have different source directories. These arrays are not merged in the way ordinary object properties might be merged.
> callout note > Keep shared policy in the base file and package boundaries in child files. This makes it clear which settings are global and which settings describe one package's inputs and outputs. > ```
Checking inherited settings
Run the compiler from a package directory or pass its configuration explicitly:
npx tsc --project packages/api/tsconfig.json --noEmit
npx tsc --project packages/api/tsconfig.json --showConfigThe first command checks the package without emitting JavaScript. The second prints the effective configuration after inheritance and overrides. Use it when a setting appears to be ignored.
| Setting | Usually belongs in | Reason |
|---|---|---|
| `strict` | Base configuration | Every package should use the same safety rules |
| `rootDir` | Package configuration | Each package has its own source directory |
| `outDir` | Package configuration | Output must not overlap another package |
| `target` | Base configuration | Packages should agree on supported JavaScript |
Exercise
A package extends ../../tsconfig.base.json, but its source files are not included. What should you check first?
Show answer
Check that the package configuration has the correct include, files, or exclude settings. The child project must select its own files, and an inherited file list may not match its directory.