以下是演示目录:
- rootDir
- node_modules
- src
- index.ts
- types
- global.d.ts
- package.json
- README.md
- tsconfig.json
// index.ts
const _var = global_var; // `global_var` exist in runtime certainly and it is from template or other script.
// global.d.ts
declare const global_var: any;
// tsconfig.json
{
"compilerOptions": {
"target": "ESNext",
"sourceMap": true,
"module": "esnext",
"moduleResolution": "node",
"noImplicitAny": true,
"noUnusedLocals": true,
"baseUrl": ".",
"typeRoots": [
"./types"
],
"types": [
"global"
]
},
"include": [
"./src/**/*.ts"
],
"compileOnSave": false
}
现在我在global_var
文件index.ts
中的Cannot find name 'global_var'.ts(2304)
上遇到类型错误。很显然,tsconfig.json
文件出了点问题……但是这有什么问题呢?
然后,我尝试修复typeRoots
和types
的值,并通过以下操作得到确定:
// tsconfig.json
{
"compilerOptions": {
...
"typeRoots": [],
"types": [
"./types/global"
]
},
"include": [
"./src/**/*.ts"
],
"compileOnSave": false
}
这与上面有什么区别? typescript
找不到第一个tsconfig.json文件跟随/types/global
路径的声明文件吗?如果没有,那是什么?