I am new to Typescript
and I'm trying to import a react-bootstrap Button
using
case 1: import {Button} from 'react-bootstrap/lib/Button'
case 2: import * as Button from 'react-bootstrap/lib/Button'
Both the ways dont throw any error on import statement but throws error when this Button is rendered using <Button bsClass="glyphicon glyphicon-new-window"></Button>
In case 1 there is no compile time error but Button is undefined
In case 2 Typescript throws the following compile time error TS2604: JSX element type 'Button' does not have any construct or call signatures.
Although this works in JS import Button from 'react-bootstrap/lib/Button'
. Now I'm not able to figure out why any of the method aint working and what is the difference between the two methods?
Let's say the below are the exports of a
button.ts
file:Case 1
Using
import { Button } from 'button'
will give you theButton
function.Cas 2
Using
import * as Button from 'button'
will give you anObject
locally namedButton
with two members:Button
andToggle
. BasicallyYou can find more information about the module syntax here.
Since you explicitly asked about TypeScript, I guess you also might run into the issue why you no longer can do
import React from ' react'
and instead have to use the* as <x>
syntax:This is due to the fact that TypeScript adheres the ESModule specification. Babel (previously) did some magic under the hood for us developers that basically was an interop between ESModules and CommonJS modules.
It depends on what kind of object is exported from the certain module. In a simple words. Lets say module "a" exported some object:
In case of
Button
will befunction() { some button construction code}
In case of
Button
will be whole exported object:For more details you can check the documentation.