https://github.com/Microsoft/TypeScript/issues/9731 https://github.com/Microsoft/TypeScript/issues/11917 https://github.com/s-panferov/awesome- typescript-loader/issues/492 tsconfig-如何忽略@ types/whatever/node_modules用于特定目录? 我的环境详细信息 节点":"8.6.0",打字稿:"2.8.3","awesome-typescript-loader":"5.0.0","webpack":"4.8.3",解决方案为此,我发现一个解决方案是在tsconfig.json paths设置中指定node_modules/@types目录.这是我在tsconfig.json中更改的代码段,您应该能够适应您的用例.我需要修改我的baseUrl和paths设置. ... "baseUrl": ".", "paths": { "@/*": ["./src/*"], "*": ["./node_modules/@types/*"] } ...在我的情况下,我在TS项目中使用绝对URL,因此我的本地文件都相对于@/.我认为,如果您不使用这样的绝对网址,则应在配置中输入以下内容: ... "baseUrl": ".", "paths": { "*": ["./src/*", "./node_modules/@types/*"] } ...这是我完整的tsconfig.json,其中可能包含很多不相关的信息,仅供参考.{ "compilerOptions": { "outDir": "./build/", "sourceMap": true, "allowJs": true, "checkJs": true, "jsx": "react", "target": "es2017", "module": "commonjs", "moduleResolution": "node", "allowSyntheticDefaultImports": true, "strict": true, "noUnusedLocals": true, "noUnusedParameters": true, "removeComments": false, "preserveConstEnums": true, "skipLibCheck": true, "experimentalDecorators": true, "esModuleInterop": true, "baseUrl": ".", "paths": { "@/*": ["./src/*"], "*": ["./node_modules/@types/*"] } }, "include": ["**/*", "*"], "exclude": ["build", "node_modules", "coverage"]}I have run into a situation where a type definition in node_modules/@types is installing its own @types dependencies, and these "nested" @types conflict with my top level @types.@types|-angular //v1.5|-angular-ui-bootstrap |-node_modules |-@types |-angular //v1.6How can I exclude node_modules/@types/**/node_modules in my tsconfig?One caveat - I am using awesome-typescript-loader, which may have some limitations.What I've tried:1 - file glob in the exclude property to exclude the nested node_modules compilerOptions.exclude: '../node_modules/@types/**/node_modules'2 - declaring types explicitly compilerOptions.types: ['angular', 'angular-ui-bootstrap']3 - file glob in the typeRoots to exclude nested node_modules compilerOptions.typeRoots: ['../node_modules/@types/**/!(node_modules)']What I've learned1 - exclude doesn't seem to work with @types2 - including a type with "types" means including its dependent @types3 - typeRoots doesn't seem to work with file globs (or I'm writing the glob wrong)Related:Exclude @types typings in installed dependencieshttps://github.com/Microsoft/TypeScript/issues/9731https://github.com/Microsoft/TypeScript/issues/11917https://github.com/s-panferov/awesome-typescript-loader/issues/492tsconfig - How to ignore @types/whatever/node_modules for a specific directory?Details on my environment"node": "8.6.0","typescript: "2.8.3","awesome-typescript-loader": "5.0.0","webpack": "4.8.3", 解决方案 A solution I found for this was to specify the node_modules/@types directory in the tsconfig.json paths setting.Here's the snippet I changed in my tsconfig.json, which you should be able to adapt for your use case. I needed to modify my baseUrl and my paths settings. ... "baseUrl": ".", "paths": { "@/*": ["./src/*"], "*": ["./node_modules/@types/*"] } ...In my case I'm using absolute urls in my TS project, so my local files are all relative to @/. I think if you are not using an absolute url like that, you should have something like the following for your config: ... "baseUrl": ".", "paths": { "*": ["./src/*", "./node_modules/@types/*"] } ...Here's my complete tsconfig.json, which probably has a lot of irrelevant information, for reference.{ "compilerOptions": { "outDir": "./build/", "sourceMap": true, "allowJs": true, "checkJs": true, "jsx": "react", "target": "es2017", "module": "commonjs", "moduleResolution": "node", "allowSyntheticDefaultImports": true, "strict": true, "noUnusedLocals": true, "noUnusedParameters": true, "removeComments": false, "preserveConstEnums": true, "skipLibCheck": true, "experimentalDecorators": true, "esModuleInterop": true, "baseUrl": ".", "paths": { "@/*": ["./src/*"], "*": ["./node_modules/@types/*"] } }, "include": ["**/*", "*"], "exclude": ["build", "node_modules", "coverage"]} 这篇关于如何排除`node_modules/@ types/**/node_modules`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-28 03:20