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

13,333

Solution 1

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.

Solution 2

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.

Share:
13,333
Jeanluca Scaljeri
Author by

Jeanluca Scaljeri

Updated on July 31, 2022

Comments

  • Jeanluca Scaljeri
    Jeanluca Scaljeri almost 2 years

    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?