Proposal for enhancing ways to extend tsconfig file · Issue #56436 · microsoft/TypeScript (original) (raw)
🔍 Search Terms
"extends", "merge", "compilerOptions", "paths", "outdir", "include", "exclude", "typeRoots", "references", "tsconfig", "ability to merge tsconfig", "ability to make tsconfig paths relative to final config"
✅ Viability Checklist
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
⭐ Suggestion
Today when you extend a config the options written in the final config override the options from the base config. Here is sample of what happens today:
Here is what it would mean that project1 tsconfig was written as:
Over years users have asked us for ability to create a final config that adds to base config instead of overriding. This is extremely useful for paths
from compiler options or say references
that specify project references.
This would allow to users to write a base config that contains common options that shouldn't have to be copied in all projects and yet allowing projects to specify there additional specific options.
For example in above sample, one would expect a way to write config such that final paths
property is:
Here is the proposal to add merge
as root level config property that specifies options that need to merged instead of completely overwritten in the final config.
The values specified in merge can be root level properties: files
, include
, exclude
and references
or option names from either compilerOptions
, watchOptions
or typeAcquisition
which are lists or object type eg. paths
, typeRoots
, libs
etc.
From the above example writing the project1
config as:
per @andrewbranch 's suggestion here: #56436 (comment)
other option is to write config as:
Would mean that project1 wrote the tsconfig as:
Note how the paths still remain relative to config they are defined in but allowing one to add more properties and if say baseConfig also had rule for @environment/*
path mapping it is overwritten by the project1 specific. That is it does not recusively merge property values of path mappings.
Apart from having ability to specify specific options that get merged, users have often asked us for a way to specify config such that base config will specify option say outDir
and instead of it being relative to the base config, it needs to be relative to project's tsconfig.
For example in the above example, instead of all projects output going into /temp/test/dist
the output to be inside each dist
folder of each project.
Apart from outDir
this would also help in writing base configs such that extending base.tsconfig.json
would include project's src
files and extending base.test.tsconfig.json
would include tests
files
With below configs:
Today the resulting config of project1 is:
Proposal here is to add property raw
with names of properties in base config such that extending it would mean as if project extending it specified them, making them relative to final config instead of the base config.
So with change to base.tsconfig.json as:
per @andrewbranch suggestion option 2 to consider:
would result in final project1 config as if it wrote:
and another project2 say with merge
property as:
would result in configuration as if written as:
The property raw
can be used for:
- Options that are paths like:
outDir
,declarationDir
etc - Options that list of paths like:
rootDirs
,typeRoots
fromcompilerOptions
orexcludeFiles
,excludeDirectories
fromwatchOptions
include
andexclude
root config properties. (files
andreferences
are not included here as not sure if they really make sense?)paths
is a special option where normally the base path (propertypathsBasePath
) is relative to config they are declared in. So addingpaths
toraw
would mean that path mapping is done relative to config file extending the root config file.
There was consideration of specifying raw
extend as part of config extending root config but that would mean either writing something like "rawExtend": ["outDir", "typeRoots"]
or "merge": [{ "name": "include", "kind": "raw" }]
in each config defeating the purpose of concise extend. Any feedback on that?
Should merge
be in root config: probably not because if project wants to override or add should be part of project decision and it would need to add properties to list only if that config specifies to making this property of project config makes more sense.
TODO:
What happens to raw
and merge
when multi-level extending is in play. Probably they need to be merged instead of overwritten but need to think about that.
Here are issues this would help with:
#44589
#27098
#20110
#29172
📃 Motivating Example
A way to specify say project specific "paths" mapping without having to rewrite all the mappings from the base config.
And a way to specify root config that can specify include
and/or outDir
and not needing to write than in each project.
💻 Use Cases
- What do you want to use this for?
- What shortcomings exist with current approaches?
- What workarounds are you using in the meantime?