【问题标题】:Missing interface from installed packages (Typescript)已安装包中缺少接口(Typescript)
【发布时间】:2021-07-01 03:42:18
【问题描述】:

我最近一直在尝试习惯使用 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 开发人员一定错过了这一点,但后来我安装了另一个不同的包,并且出现了同样的问题。某些道具不见了!所以我的打字稿设置一定有问题,而且我现在完全不知所措,因为我刚开始学习打字稿。遇到此类问题的一般做法是什么?

【问题讨论】:

    标签: reactjs typescript pusher


    【解决方案1】:

    很可能您只需要安装相关软件包的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
    

    【讨论】:

    • 我明白了,当我们使用 Typescript for React 时,这是常见的事情吗?我会尽快试用并通知您,谢谢!
    • 为什么他们不开始呢?当他们可以将其添加到界面中时,似乎没有必要安装更多的东西?
    • 解决了,有点。事实证明,Pusher 很久以前就已经弃用了加密道具。 github.com/DefinitelyTyped/DefinitelyTyped/commit/… 感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-08
    • 1970-01-01
    • 2017-01-10
    • 1970-01-01
    相关资源
    最近更新 更多