运行 tsc 时,我得到了很多
TS2688: Cannot find type definition file for 'someLibrary'
这些库来自node_modules
。我尝试在 tsconfig 中排除 node_modules
和 skipLibCheck
,但它们都不适合我。知道为什么会发生这种情况吗?
这是我的 tsconfig.json
{
"ts-node": {
"files": true,
"emit": true,
"compilerHost": true
},
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"target": "es2016",
"sourceMap": true,
"typeRoots" : ["./node_modules","./node_modules/@types"],
"jsx": "react",
"allowSyntheticDefaultImports": true,
"skipLibCheck": true,
"esModuleInterop": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "commonJS",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
},
"exclude": [
"node_modules"
]
}
问题是错误的
typeRoots
。应该使用默认值 ./node_modules/@types
在
ts.config.json
中,您已默认放置 typeRoots。像这样:
"compilerOptions": {
...
"typeRoots": ["./node_modules/@types"]
}
参加聚会有点晚了,但我在项目目录之外安装非全局 npm 模块时遇到了这个问题。我花了太长时间才意识到。
对于我的NestJs应用程序
"typeRoots": ["./node_modules/@types"]
没有解决问题
这个问题是因为我有一个包含 DevDependency“@types/x”的包,但我在 package.json 中没有该类型的 DevDependency 的依赖项。