gpt4 book ai didi

javascript - import * as module 和 javascript 中的 import module 有什么区别

转载 作者:搜寻专家 更新时间:2023-10-30 21:17:03 24 4
gpt4 key购买 nike

当我写 typescript 时:

我有这个代码:

import * as express from 'express'

然后系统给我一个错误:

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.

所以,我改为:

import express from 'express'

它们有什么区别,为什么第一种方式不能调用和构造?

最佳答案

what is the difference between them

  • * as express 将导入模块的全部内容
  • express 是只会导入默认导出

why the first way can not called or constructed?

根据 ES 规范,模块本身是不可调用的。所以你将无法执行 express() 即函数调用。因此它必须映射到模块的成员,在本例中是default 导出成员🌹

关于javascript - import * as module 和 javascript 中的 import module 有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55914343/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com