使用 Window.chrome 类型

1 google-chrome-extension typescript-typings typescript2.0

我安装了这个 NPM 包: https: //www.npmjs.com/package/@types/chrome

现在我的代码中有这个:

declare var chrome;

chrome.runtime.onConnect.addListener(function (port) {
  console.log('extension is connected to port:', port);
  port.onMessage.addListener(function (msg) {
    console.log('message from port:', msg);
  });
});
Run Code Online (Sandbox Code Playgroud)

当我删除声明语句时,我的 IDE (Webstorm) 不会给我任何关于使用哪种类型的建议。

有谁知道如何在项目中正确引用/合并 chrome 类型?

Gra*_*nga 6

安装镀铬定义。

npm install @types/chrome --save-dev
Run Code Online (Sandbox Code Playgroud)

然后,在您的打字稿文件之一中引用它。我在我的custom-typings.d.ts

///<reference types="chrome"/>

...
Run Code Online (Sandbox Code Playgroud)