【问题标题】:reactstrap with typescript, define ambient modulereactstrap with typescript,定义环境模块
【发布时间】:2019-10-14 12:16:56
【问题描述】:

我想将reactstrap 与打字稿一起使用。

这些分型有分型:https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/reactstrap/index.d.ts

Reactstrap 在页面上作为Reactstrap 全局变量可用。我认为将它添加到环境模块中就像添加 reactreact dom 一样简单

这是我的global.d.ts

import * as react from "react"
import * as react_dom from "react-dom"
import * as reactstrap from "reactstrap"

declare global {
    type React = typeof react
    type ReactDOM = typeof react_dom
    type Reactstrap = typeof reactstrap
}

我可以在不导入的情况下使用React.Component 和 jsx,它在全球范围内可用。但我不能使用Reactstrap.Alert,tsc 说Reactstrap.Alert 是一种类型,但用作值。

我可以在 d.ts 文件中手动添加此字符串 export as namespace Reactstrap,但它看起来像 hack,并且此更改未存储在我的 git 存储库中。是否可以从一个 d.ts 导出所有类并将其放入另一个的命名空间?

【问题讨论】:

    标签: typescript reactstrap


    【解决方案1】:

    我在global.d.ts 旁边创建了文件_reactstrap.d.ts,内容如下:

    export * from "reactstrap"
    
    export as namespace Reactstrap;
    

    并修改global.d.ts如下:

    import * as react from "react"
    import * as react_dom from "react-dom"
    import * as reactstrap from "./_reactstrap"
    
    declare global {
        type React = typeof react
        type ReactDOM = typeof react_dom
        type Reactstrap = typeof reactstrap
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-12
      • 1970-01-01
      • 2012-10-12
      • 2016-07-17
      • 1970-01-01
      • 2021-11-26
      • 1970-01-01
      相关资源
      最近更新 更多