在typescript中禁用特定的全局var声明检查

时间:2016-07-12 13:44:35

标签: google-chrome typescript global-variables webstorm

我在WebStrom IDE中为Chrome编写扩展程序。问题是,打字稿编译器一直抱怨未定义' chrome变量'。如果我使用declare var chrome: any;将其静音,那么它会发出抱怨,chrome变量可能尚未初始化'。

有解决方案吗?在tsconfig中明确定义全局,或者Chrome运行时可能有特定的typedef文件?

1 个答案:

答案 0 :(得分:1)

Chrome运行时有一个特定的typedef,可以准确解决您的问题:https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/chrome/chrome.d.ts。如果你添加它,一切都应该很好。

您需要使用Typings进行设置,而不是直接进行设置。不清楚你是否已经熟悉它,这应该需要:

# Install Typings on your system
npm install typings --global

# Install the Chrome type definitions from DefinitelyTyped ('dt')
typings install dt~chrome --global --save

更一般地说,上面的declare var语句足以禁止TypeScript检查chrome变量并使其成功编译,如果您不想使用上面正确的类型定义(或如果你在其他情况下找不到这样的话。)

你仍然看到的警告只来自WebStorm。我不确定究竟需要什么来保持沉默,但我希望将它移到外部类型定义才能工作。只需将相同的代码 - declare var chrome: any - 添加到新的chrome.d.ts文件中,并确保它包含在您的编译文件中(可能会自动发生)。应该这样做。