最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

typescript - what different between import * as module and import module in javascript - Stack Overflow

programmeradmin0浏览0评论

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 give alias name. – random Commented Apr 30, 2019 at 5:14
Add a ment  | 

1 Answer 1

Reset to default 8

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

发布评论

评论列表(0)

  1. 暂无评论