我正在尝试在打字稿中分配自定义全局变量。我的项目结构如下:
- config/
- src/
- types/
- custom.d.ts
- index.ts
- package.json
- tsconfig.json
custom.d.ts
文件中的文件如下:
declare global {
namespace NodeJS {
interface Global {
customvar: string
}
}
}
export default global;
并在我的tsconfig.json
中:
{
...,
"typeRoots": [
"types"
],
"types": [
"node"
]
}
在我的index.ts
中,当我尝试以下操作并运行它时:
global.customvar = "test";
它引发错误:
index.ts:1:8 - error TS2339: Property 'customvar' does not exist on type 'Global & typeof globalThis'.
我做错了什么?我的customvar
甚至已经显示为自动完成功能:
我尝试了可以在网上找到的解决方案,但似乎没有解决我的问题。我错过了什么?任何解决方案/建议都将不胜感激。
答案 0 :(得分:0)
此问题已解决。问题既不是来自打字稿,也不是来自结构本身。但是,它来自ts-node
。由于我使用ts-node
运行脚本,因此我的设置中缺少一些配置。
解决方案可以在这里找到:https://github.com/TypeStrong/ts-node#help-my-types-are-missing