5

I'm writing a TypeScript/React Web app that has the following top level directory structure:

├── jest.config.ts
├── node_modules
├── package.json
├── package-lock.json
├── public
├── src
├── tsconfig.json
└── webpack.config.js

All TypeScript source files are contained in the src/ directory, but I want to ignore a single file (and potentially more in the future) from compilation/type checking.

I'm getting an error in this file, but I don't want to use the file in the app. I also don't want to remove the file as it's part of a module used by another app.

Error

error TS2307: Cannot find module 'x' or its corresponding type declarations.

Can you help me do this? Here is the tsconfig.json file I'm working with:

tsconfig

{
  "compilerOptions": {
    "allowJs": true,
    "emitDecoratorMetadata": true,
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "forceConsistentCasingInFileNames": true,
    "jsx": "react",
    "module": "es6",
    "moduleResolution": "node",
    "outDir": "./dist",
    "rootDir": "./src",
    "skipLibCheck": true,
    "strict": true,
    "target": "es5"
  },
  "exclude": [
    "dist/",
    "jest.config.ts",
    "webpack.config.js"
  ]
}

What I Tried

I read that exclude directive does not work with rootDir, but I tried the following anyway:

Add the path to the file in question to the `exclude` array.

That didn't work (I didn't really expect it to). So, while the file path was still in the exclude array:

Stop using the `rootDir`, instead add `include: ["src/"]`.

No change, I still get an error on the file I don't want to compile.

From the CLI

I tried to excludeFiles from the command line using both the relative path from the project's root, and from the rootDir in the tsconfig.

tsc -w --noEmit --excludeFiles path/to/file.ts

Exclude the file by name only.

tsc -w --noEmit --excludeFiles file.ts

Exclude the directory of the file (path from project root & rootDir).:

tsc -w --noEmit --excludeDirectories path/to/containing/directory

Search

I've also tried a plethora of Web searches, and I've searched this site.

@ts-ignore

The // @ts-ignore comment can mute the error, but I don't think this is a good solution for my use case (as this change would be committed back to the module, and in other project I would want the error to be raised).

TIA

Jason
  • 2,382
  • 1
  • 16
  • 29

1 Answers1

0

Here's what I did to achieve the goal.

In the following example, I want to compile all files in the src/ directory, except one (exclude.ts).

The set up:


├── node_modules
│   └── typescript
├── package.json
├── package-lock.json
├── src
│   ├── exclude.ts
│   └── include.ts
└── tsconfig.json

My tsconfig.json

{
  "compilerOptions": {
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "module": "commonjs",
    "outDir": "dist",
    "skipLibCheck": true,
    "strict": true,
    "target": "es2016"
  },
  "exclude": [
    "src/exclude.ts"
  ],
  "include": [
    "src/"
  ]
}

This works. When I run the TypeScript compiler, I only see the include.js file in the dist/ directory. I thought I tried it before (as I state in my earlier question), but I must have missed something.

Jason
  • 2,382
  • 1
  • 16
  • 29