TypeScript 找不到自定义类型定义文件
我在 VS Code 1.30.2 中使用 typescript 3.3.3。TypeScript 无法找到位于 src/@types 的自定义类型定义文件。尽管据我所知,在最新版本的 TS 中不需要配置 typeRoots,但我仍然配置了,TypeScript 仍然找不到 index.d.ts 文件。文件夹名和模块名是匹配的。
.
├── config.ts
├── node_modules
├── ormconfig.json
├── package.json
├── project_manager
├── src
├── tsconfig.json
├── tslint.json
└── yarn.lock
tsconfig.json
{
"compileOnSave": false,
"compilerOptions": {
"lib": [ "es2015", "dom" ],
"target": "es2015",
"module": "commonjs",
"resolveJsonModule": true,
"allowJs": true,
"jsx": "react",
"outDir": "./dist",
"strict": true,
"noImplicitAny": true,
"noImplicitThis": true,
"skipLibCheck": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"pretty": true,
},
"exclude": [
"node_modules",
"project_manager",
"dist",
"@types"
]
}
答案#1
让这个工作起来可能感觉就像把一个魔法咒语恰到好处地念出来。经过为一个最近的项目进行一些探索,我在 GitHub 上发现了如下配置:
- 将你的
baseUrl设置为. - 为包含你的类型覆盖的目录添加一个
paths映射 - 为自定义类型创建
types-dir/module-name/index.d.ts文件
tsconfig.json 的最小子部分大致如下:
{
"compilerOptions": {
"baseUrl": ".",
"outDir": "dist",
"rootDir": "src",
"paths": {
"*": ["./types/*"]
}
}
}
在你的情况下,你可能希望 paths 指向 ./src/@types/*。
