7

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

3
  • I have the same problem: trying to exclude a single file when compiling from the cli (without tsconfig). Did you find a solution? Commented May 5, 2022 at 13:06
  • 1
    @lonix I have not. Those I've asked IRL, all think it's possible, but no one has an actual solution. Commented May 5, 2022 at 19:00
  • @Ionix, I posted a solution. Commented Aug 27, 2022 at 13:34

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.