Alexander Kondaurov
Alexander Kondaurov

Reputation: 4123

reactstrap with typescript, define ambient module

I want to use reactstrap with typescript.

There are typings for these typings: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/reactstrap/index.d.ts

Reactstrap is availabe on page as Reactstrap global variable. I thought adding it in ambient module would be as simple as adding react and react dom

Here's my 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
}

I can use React.Component and jsx without importing, it's available globally. But i can't use Reactstrap.Alert, tsc says thatReactstrap.Alert is a type but used like a value.

I can manually add this string export as namespace Reactstrap in a d.ts file, but it looks like a hack, and this changes are not stored in my git repo. Is it possible to export all classes from one d.ts and put it into namespace of another?

Upvotes: 0

Views: 624

Answers (1)

Alexander Kondaurov
Alexander Kondaurov

Reputation: 4123

I've created file _reactstrap.d.ts next to global.d.ts with this content:

export * from "reactstrap"

export as namespace Reactstrap;

And modified global.d.ts as follows:

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
}

Upvotes: 0

Related Questions