我最近一直在努力习惯使用Typescript。我注意到,每次我安装并使用软件包时,某些道具都会有Typescript向我尖叫,说它不存在于软件包开发人员所做的界面中。
这里有一个例子,我安装了Pusher js,然后像这样实现它;
var pusher = new Pusher("API_KEY", {
cluster: "ap1",
encrypted: true,
});
然后加密道具上会出现错误:
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)
所以我去了Pusher js node_modules中的options.d.ts,确信接口中没有定义足够的加密。我想我可以编辑那个,但很明显,当我更新包时,它会被覆盖。
我的第一个想法是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