tsc not excluding npm linked node_modules

Viewed 1389

The problem: When type checking my app using tsc I am getting errors for npm linked modules.

Here is my type-check command:
"type-check": "tsc --noEmit -p tsconfig.json"

and here is my tsconfig.json:

{
    "compilerOptions": {
        "outDir": "./dist/",
        "sourceMap": true,
        "noImplicitAny": false,
        "allowJs": true,
        "module": "commonjs",
        "target": "es6",
        "jsx": "react",
        "esModuleInterop": true,
        "experimentalDecorators": true,
        "skipLibCheck": true
        //"importsNotUsedAsValues": "error"
    },
    "exclude": ["./node_modules/*"] // I've also tried node_modules, /node_modules, node_modules/
}

The command will fail due to type errors in one of my node_modules which is connected via npm link.

Any way to exclude this module from type check


Edit:

I have also tried using a double globstar (**) e.g.

"exclude": ["./node_modules/**/*"]

and this gives me the same incorrect result.

2 Answers

If you are using webpack try to add symlinks: false in resolve. That solved similar issue for me.

resolve: {
    // ...  
    symlinks: false,
},

If you are using purely tsc try (I haven't tried this):

preserveSymlinks: true

From my experience, npm/yarn link never works well. Something is always broken: webpack or TypeScript or the linked package simply requires a build step, which they almost all do nowadays.

Check out relative-deps, which aims to solve this problem.

Related