Exclude @types typings in installed dependencies
Asked Answered
V

3

32

Is it possible to exclude global typings in installed dependencies?

I installed a local dependency. This also copied the node_modules folder of that dependency. This node_modules folder holds installed @types typings. These conflict with the @types typings installed for the main project.

Eg. project-path/node_modules/local-dependency/node_modules/@types/react conflicts with project-path/node_modules/@types/react.

Is it possible to make the typescript compiler ignore the typings in that local dependency?

Velvetvelveteen answered 9/2, 2018 at 10:42 Comment(2)
I've answered a similar question. I fixed the issue by specifying ./node_modules/@types/* in the paths key of my tsconfig.json.Cottier
Possible duplicate of How to exclude `node_modules/@types/**/node_modules`?Cottier
D
9

Alternatively, use "skipLibCheck": true in the compiler options.

Dichroic answered 12/9, 2020 at 11:24 Comment(0)
S
7

Did you try to use an empty array for types option?

{
  "compilerOptions": {
    ...,
    "types": []
  }
}

This disables automatic inclusion of types.

See the TypeScript documentation for more details.

Subchloride answered 19/7, 2019 at 13:46 Comment(0)
B
-4

You may be able to use the include and exclude properties in your tsconfig.json for more fine-grained control over what files the TypeScript compiler includes:

http://www.typescriptlang.org/docs/handbook/tsconfig-json.html

The "include" and "exclude" properties take a list of glob-like file patterns. The supported glob wildcards are:

  • * matches zero or more characters (excluding directory separators)
  • ? matches any one character (excluding directory separators)
  • **/ recursively matches any subdirectory

So perhaps you could do something like:

{

    ...    

    "exclude": [
        "node_modules"
    ]
}

Or, depending on yours needs, you could design a more targeted glob pattern that only excludes the node_modules directory of the one dependency giving you problems.

If you have control over the local-dependency module in your example, a better solution would be to update either the dependency or your app to use the same the version of the @types/react module, delete your npm modules, and do a fresh npm install. This should allow npm to install a single, shared version of the module, removing any possibility for conflicts.

Beckett answered 9/2, 2018 at 12:23 Comment(1)
I tried putting "exclude": ["node_modules/@types/node/index.d.ts"] but it doesn’t make a difference. It seems that node_modules/@types is special—the only way to do an exclusion is to use the types property and explicitly include everything except what I don’t want which means updating the list every time an indirect dependency changes. I.e., there might not be support in TypeScript for exclusion for node_modules/@types. If there is, this isn’t it.Aerodynamics

© 2022 - 2024 — McMap. All rights reserved.