tsconfig.json parameters cannot be extended, only overwritten
Something I believe should be more clearly spelled out in the Typescript documentation is how the extends
option in the tsconfig.json
file handles overrides. One aspect that cost me more hours than I care to admit is parameters like include
or files
are not merged, but instead overwritten by child configurations.
For more context about the Typescript team’s decision, see this github issue
In many cases, this behavior isn’t really a problem, as overwriting a simple property like strict
is likely what you want. However, if the property is something like an array, this can be pretty limiting. In my case, I wanted to have a global d.ts
file that holds all the definitions for any untyped dependencies we have in our Nrwl monorepo. I tried adding this file (like the one below) to the tsconfig.base.json
files
parameter, but was puzzled when it wouldn’t be picked up by VSCode.
/// global.d.ts
declare module 'backo';// tsconfig.base.json
{
... // omitted
files: ["global.d.ts"]
}
Turns out that every auto-generated config in each Angular library sets the files
parameter, which means the base setting gets deleted.
// Example child tsconfig.json
{
"extends": "../../../tsconfig.base.json",
"compilerOptions": {
"strict": true
},
"files": [], // goodbye global.d.ts
"references": [{
"path": "./tsconfig.lib.json"
},
{
"path": "./tsconfig.spec.json"
}],
}
While all child tsconfig.json
files are inheriting from the base file, they all include explicit settings for files
, which meant that the reference in the base config was useless.
Unfortunately, the only solution as far as I can tell is to add the reference to each child’s config.
{
... // omitted
"files": ["../../../global.d.ts"]
}
Not very DRY, but that’s how it is. Since monorepos are growing in popularity, perhaps the Typescript team will revisit their decision to only support overwriting base configuration.