reactstrap with typescript,定义环境模块

Posted

技术标签:

【中文标题】reactstrap with typescript,定义环境模块【英文标题】:reactstrap with typescript, define ambient module 【发布时间】: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 导出所有类并将其放入另一个的命名空间?

【问题讨论】:

【参考方案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

【讨论】:

以上是关于reactstrap with typescript,定义环境模块的主要内容,如果未能解决你的问题,请参考以下文章

typeScrip泛型

关于 Reactstrap 安装

Springboot + mybatis + React+redux+React-router+antd+Typescript: React+Typescrip项目的搭建

如何更改 reactstrap 下拉图标?

如何反转reactstrap中的列?

尝试导入错误:“reactstrap”不包含默认导出(导入为“表”)