Sub topic 2
A shared base config with `extends`
Assume you already know basic TypeScript syntax, tsconfig.json, and commands such as tsc --build. This chapter focuses on sharing compiler settings across several projects without making the projects identical.
Why use a base configuration?
A multi-package repository often needs the same language and module settings everywhere. For example, every package might use strict checking, ES2022 syntax, and Node's modern module rules.
Copying those settings into every tsconfig.json creates drift. One package may accidentally use a different module mode. Another may forget strict. The extends property lets a project inherit settings from another configuration.
Keep the base configuration at the repository root:
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"strict": true,
"skipLibCheck": true
}
}A package can extend it and add settings that belong only to that package:
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"composite": true,
"declarationMap": true,
"rootDir": "src",
"outDir": "dist"
},
"include": ["src"]
}The path in extends is relative to the configuration file containing it. In this example, packages/core/tsconfig.json moves up two directories to reach tsconfig.base.json.
Separate shared settings from project settings
The base file should normally contain settings that apply to every project. A package configuration should contain its own source boundaries and output locations.
| Setting | Good location | Reason |
|---|---|---|
| `strict` | Base configuration | Every project should use the same type-checking policy |
| `target` | Base configuration | All projects should agree on JavaScript syntax |
| `composite` | Package configuration | Each referenced project opts into composite rules |
| `rootDir` | Package configuration | Source layout can differ between projects |
| `outDir` | Package configuration | Outputs must stay separate |
| `include` | Package configuration | Each project owns a different set of files |
include, exclude, and files in the child configuration replace corresponding settings from the base configuration rather than combining with them. This is another reason to keep source-file selection in each project.
A base configuration also should not accidentally compile the whole repository. It can contain only compilerOptions, leaving each package to define its own include.
Understand path resolution
Relative paths in a configuration are resolved relative to the configuration file where they appear. Therefore, putting outDir in the base file may place every package's output relative to the root configuration, which may not match the intended layout. Define package-specific paths in package configurations.
The extends relationship shares options. It does not declare a build dependency. Use the references field for that relationship:
flowchart TD
A[tsconfig.base.json] --> B[packages/core/tsconfig.json]
A --> C[packages/app/tsconfig.json]
B --> CCheck the effective configuration
When a setting seems wrong, inspect the configuration after inheritance is applied:
tsc --showConfig -p packages/core/tsconfig.jsonThis prints the effective options and file list. It is useful for finding an incorrect relative path, an overridden option, or a missing include.
Exercise
Should include: ["src"] normally go in the shared base configuration or in each package configuration?
Show answer
Put it in each package configuration because each project has its own source boundary, and a child include replaces rather than combines with the base setting.