Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what different between import * as module and import module in javascript

When I write typescript:

I have this code:

import * as express from 'express'

and the system gives me an error:

Type originates at this import. A namespace-style import cannot be called or constructed, and will cause a failure at runtime. Consider using a default import or import require here instead.

So, I change to:

import express from 'express'

what is the difference between them, why the first way can not called or constructed?

like image 802
user504909 Avatar asked Sep 23 '26 20:09

user504909


1 Answers

what is the difference between them

  • * as express will import the entire contents of a module
  • express is will import the default export only

why the first way can not called or constructed?

A module itself is not callable as per the ES spec. So you wouldn't be able to do express() i.e. a function invocation. Therefore it must mapped to a member of the module, in this case the default export member 🌹

like image 145
basarat Avatar answered Sep 25 '26 08:09

basarat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!