使用 typeof Window.chrome

Use typeof Window.chrome

我安装了这个 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);
  });
});

当我去掉 declare 语句时,我的 IDE (Webstorm) 没有给我任何关于使用哪种类型的建议。

有谁知道如何在项目中正确 reference/incorporate chrome 打字?

安装 chrome 定义。

npm install @types/chrome --save-dev

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

中进行
///<reference types="chrome"/>

...