最佳答案
在成功编译了 TypeScript 项目之后,我打算使用 ts-node
以 VS Code 的调试模式运行它。问题是,ts-node
找不到我创建的 d.ts
文件(而 tsc
没有问题)。
项目结构如下:
/
conf/
dist/
src/
types/
package.json
tsconfig.json
tsconfig.json
相关参赛作品包括:
{
"compilerOptions": {
"target": "es2017",
"module": "commonjs",
// "lib": [],
"sourceMap": true,
"outDir": "dist",
"rootDir": "src",
"moduleResolution": "node",
"baseUrl": ".",
"paths": {
"*": [
"node_modules/*",
"src/types/*"
]
},
// "rootDirs": [],
// "typeRoots": [],
// "types": [],
},
"include": [
"src/**/*"
]
}
ts-node
找不到的定义文件是 src/types/global.d.ts
:
import { App } from '../App';
declare global {
namespace NodeJS {
interface Global {
app: App;
}
}
}
So, trying to run it with ts-node
I see:
TSError: ⨯ Unable to compile TypeScript:
src/boot.ts(15,59): error TS2339: Property 'app' does not exist on type 'Global'.
如何在全球范围内解决这个问题?我发现 /// <reference path="./types/global.d.ts" />
可以做到这一点,但是我必须使用 global.app
在每个文件中重复它。
My TypeScript version is 3.0.1