Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

relative paths for outDir and rootDir in a base tsconfig.json

Tags:

typescript

I have a base tsconfig.json where I want to have all consistent properties for all projects that reference it.

I was hoping I could persistently put the outDir, rootDir, include and excludes here:

{
  "compilerOptions": {
    "declaration": true,
    "downlevelIteration": true,
    "module": "commonjs",
    "moduleResolution": "node",
    "noUnusedLocals": true,
    "outDir": "./dist",
    "removeComments": false,
    "rootDir": "./src",
    "skipLibCheck": true,
    "sourceMap": true,
    "strict": true,
    "target": "es5"
  },
  "include": [
    "src/**/*.ts",
    "src/**/*.tsx"
  ],
  "exclude": [
    "./dist",
    "src/**/*.test.ts",
    "src/**/*.test.tsx"
  ]
}

Unfortunately

{
  "extends": "../frontend-common/typescript/tsconfig.base.json",
}

But when I try and compile a project, I get this error:

error TS6059: File 'blah.ts' is not under 'rootDir' '/path/to/tsconfig.base.json/dir'. 'rootDir' is expected to contain all source files.

File paths in tsconfig.base.json are probably unsurprisingly relative to that file's location.

Is there any way of making them relative to the tsconfig.json that references the base tsconfig.base.json?

It is annoying to have this in each tsconfig.json

  "include": [
    "src/**/*.ts",
    "src/**/*.tsx"
  ],
  "exclude": [
    "./dist",
    "src/**/*.test.ts",
    "src/**/*.test.tsx"
  ]
like image 688
dagda1 Avatar asked Sep 17 '25 03:09

dagda1


1 Answers

This has been added in TypeScript 5.5

You can now use ${configDir} as placeholder in your tsconfig.base.json:

{
    "compilerOptions": {
        "outDir": "${configDir}/dist"
    }
}
like image 130
Rupert Angermeier Avatar answered Sep 18 '25 16:09

Rupert Angermeier