无法使用TypeScript声明全局变量

时间:2017-03-06 06:30:52

标签: javascript node.js typescript typescript2.0

我正在程序中删除大多数全局变量,但有些变量总是很有用。

我已经多次查找了这些答案并放弃了自己找到答案 - 用TS声明全局变量的假设方式是这样的:

declare var x: string;

将转换为 - global.x = 'foo';

但是这个声明并没有消除Webstorm中的编译错误或IDE警告。

在我的程序中声明全局变量的最佳方法是什么?我认为最好的方法是使用.d.ts文件。

以下是我看到的警告类型:

enter image description here

1 个答案:

答案 0 :(得分:0)

不确定这是否是最好的方法:

在我的项目名为suman.d.ts的文件中,我有:

import Global = NodeJS.Global;

interface SumanGlobal extends Global {
  _writeTestError: Function,
  sumanRuntimeErrors: Array<Error | string>,
  sumanOpts: Object,
  sumanUncaughtExceptionTriggered: boolean
}

declare var global: SumanGlobal;

目前似乎有效