【发布时间】:2019-01-07 16:26:21
【问题描述】:
成功编译我的 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;
}
}
}
所以,尝试使用 ts-node 运行它,我明白了:
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 重复它。
我的 TypeScript 版本是 3.0.1
【问题讨论】:
标签: typescript typescript-typings ts-node