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?
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?
Share Improve this question asked Apr 30, 2019 at 5:12 user504909user504909 9,55916 gold badges74 silver badges113 bronze badges 1-
as
is used when you want to givealias name
. – random Commented Apr 30, 2019 at 5:14
1 Answer
Reset to default 8what is the difference between them
* as express
will import the entire contents of a moduleexpress
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