Use tsconfig flags for test files but exclude them from build

Viewed 709

I have a tsconfig.json file containing an explicit include pattern for src/**/*. I also have tests present in test/, but these are only executed using ts-jest and I never want to compile them to my dist folder or include them when bundling the source code.

This works fine, except I started getting warnings in some test files because of required flags such as esModuleInterop. Since test/ is not included in tsconfig.json, not only are the test files ignored when building, but the flags are also not active for the test files.

How can I write a tsconfig.json file that

  1. Compiles only files in src/ to dist/
  2. Still respects flags for files in test/ and tools/

I managed to put a separate tsconfig.json file in my test/ folder and yet another one in my tools/ folder but I'm wondering if it's possible to avoid this and only have a single file?

1 Answers

To my knowledge, you can't build some files and only type-check others from a single tsconfig. However, to save duplication of the tsconfig flags, you can use the "extends" option:

project/tsconfig.json:

{
  "compilerOptions": {
    "esModuleInterop": true
    // other options here
  },
  "include": ["src/**/*"]
}

project/test/tsconfig.json:

{
  "extends": "../tsconfig.json"
  // compilerOptions will be inherited
  // you may need to set "include" here relative to the "test" directory
}
Related