typescripttsc

Compiling part of Typescript project is causing an error


I have a Typescript project containing two main folders: src & test. When compiling the TS code, I want to compile just the src folder. I don't need the tests in the dist folder.

So, I set these settings in the tsconfig.json:

"rootDir": "./src",
"outDir": "./dist",

I thought that was the way to do it. But unfortunately, when I run the tsc compiler, I get an error message:

error TS6059: File '...my-proj/test/test.ts' is not under 'rootDir' '...my-proj/src'. 'rootDir' is expected to contain all source files.
  The file is in the program because:
    Matched by default include pattern '**/*'

(Another unexpected result is that in addition to compiling the src/* files to the dist folder, the compiler creates js files in the test folder, side by side with the original files.)

How can I accomplish my goal?


Solution

  • This is because you're not excluding the tests folder from typescript compilation or you haven't set the directories or files to include.

    Either include the files you want to compile or exclude the ones you don't want

    This is an example tsconfig.json to achieve it.

    {
       "include": ["src/**/*"],
       "exclude": ["node_modules", "test/**/*"],
    }