【问题标题】:Install npm package that does not have typings安装没有类型的 npm 包
【发布时间】:2023-03-27 02:11:02
【问题描述】:

我正在使用 Typescript 2.1。我正在尝试安装一个没有类型的 npm 包 (reactable)。

如果我这样做:

import * as Reactable from 'reactable' typescript 抱怨找不到这个模块。

如果我这样做:

const Reactable = require('reactable') typescript 抱怨require 被禁止。

如果我使用另一篇文章中建议的奇怪语法:

import Reactable = require('reactable'); typescript 抱怨 import 赋值不能用于定位 es2015 模块。

有没有什么方法可以导入没有类型的 npm 包?

【问题讨论】:

  • 这是TS2304 的副本吗?你试过declare var require: any吗?

标签: javascript typescript npm


【解决方案1】:

您需要声明您正在使用的内容。 例如

declare module "reactable" {
    interface ReactableProps {
        ....
    }

    interface ReactableState {
        ....
    }

    class reactable extends React.Component< ReactableProps, ReactableState> { }
}

您可以从您正在使用并需要 IDE 支持的内容开始,然后慢慢添加。

您可以查看Writing Declaration Files 了解更多信息。

【讨论】:

    猜你喜欢
    • 2018-05-25
    • 2018-03-18
    • 2018-07-29
    • 1970-01-01
    • 2022-01-24
    • 2016-03-14
    • 2018-08-24
    • 2019-06-03
    • 2020-08-11
    相关资源
    最近更新 更多