已安装包中缺少接口(打字稿)

Missing interface from installed packages (Typescript)

最近一直在努力适应使用Typescript。我注意到每次我安装并使用一个包时,某些道具都会让 Typescript 向我尖叫,说它不存在于包开发人员所做的界面中。

举个例子,我安装了Pusher-js,然后这样实现;

    var pusher = new Pusher("API_KEY", {
      cluster: "ap1",
      encrypted: true,
    });

那么加密的prop会报错:

Argument of type '{ cluster: string; encrypted: boolean; }' is not assignable to parameter of type 'Options'.
  Object literal may only specify known properties, and 'encrypted' does not exist in type 'Options'.ts(2345)

所以我去了options.d.ts in Pusher-js node_modules 果然接口里没有定义encrypted。我想我可以编辑那个,但很明显,当我更新我的包时它会被覆盖。

我的第一个想法是 Pusher-js 开发人员一定错过了这个,但后来我安装了另一个不同的包,但出现了同样的问题。缺少某些道具!所以我的打字稿设置一定有问题,自从我刚开始学习打字稿以来,我现在感觉完全不知所措。遇到这种问题一般的做法是什么?

很可能您只需要安装相关包的 types 包。

// this is an example of a default package.json 
// when you set up a new Angular-Project.
"@types/jasmine": "~3.6.0",
"@types/node": "^12.11.1"

正如您在这里看到的,有时需要安装类型包。

这是您要搜索的类型包:

npm install --save @types/pusher-js