Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending tsconfig.json file doesn't seem to extend anything

I have a project from which I need to build two different products. Say I have

./src/advanced
./src/basic

All code is written in Typescript so I need to compile this with tsc

Because of this, I created 3 tsconfig files

tsconfig-base.json

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "noImplicitAny": false,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es6",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./src",
    "lib": ["es2018", "dom", "dom", "dom.iterable", "es6"],
    "importHelpers": true,
  },
  "exclude": ["node_modules", "**/*.spec.ts","dist"]

Now to build the basic product I have

tsconfig-basic.json

{
  "extends": "./tsconfig-base.json",
  "compilerOptions": {
    "noEmitHelpers": true
  },
  "files": [
     "basic/main.ts"
  ]
}

And I compile as follows

$> tsc -p ./tsconfig-basic.json

Now I have 2 issues

1) The file basic/main.ts cannot be found, its looking in ./basic/main.ts while it should have been ./src/basic/main.ts. Why is baseUrl not prepended?

2) If (1) is fixed, the compiled files are not written to ./dist. Why is "outDir": "./dist from the base file not used here? When I add the outDir to tsconfig-basic.json it works as expected

Anyway, it looks like that extending here doesn't work, or works differently than I expect. Any suggestion how to improve my setup?

like image 477
Jeanluca Scaljeri Avatar asked Oct 11 '19 08:10

Jeanluca Scaljeri


People also ask

What is strict Tsconfig json?

TypeScript's strict mode parameter can be configurated as several individual parameters for each specific case of type checking. So, basically, if you set the parameter strict to true in tsconfig. json it means that all these strict options are set to true.

Why Tsconfig json is required?

The presence of a tsconfig. json file in a directory indicates that the directory is the root of a TypeScript project. The tsconfig. json file specifies the root files and the compiler options required to compile the project.

What is exclude in Tsconfig?

Use the exclude option in your tsconfig. json file to exclude a folder from compilation in TypeScript. The exclude option changes what the include setting finds and defaults to node_modules and bower_components .


2 Answers

There is a trick though. If you create a symlink to the base Tsconfig file from a relevant directory and extend the symlinked version rather than the original, all the paths will be resolved according to your expectations.

like image 32
Archibald Avatar answered Sep 22 '22 21:09

Archibald


1) baseUrl is only meant to be used with bundlers like webpack. See discussion on TypeScript/10866


2) This is unfortunately by design. See issue TypeScript/29172

Quote Wesley Wigham (Microsoft Employee):
Path-based compiler options (outDir, outFile, rootDir, include, files) are resolved from the config file they're found in)

You will need to repeat the outDir for every tsconfig.json file you have.

like image 108
a1300 Avatar answered Sep 21 '22 21:09

a1300