【问题标题】:Typescript `esModuleInterop` ignored with module import模块导入忽略了打字稿`esModuleInterop`
【发布时间】:2021-07-24 22:52:26
【问题描述】:

我正在尝试将此模块导入我的 Typescript 文件,但它会引发有关如何导入模块的错误。这是一个 Google Firebase 函数脚本,但我在我的项目代码的其他区域导入这个模块就好了。

10 const flamelinkApp = flamelink({
                        ~~~~~~~~~

  src/index.ts:3:1
    3 import * as flamelink from 'flamelink/app';
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    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.

NMy Typescript 配置文件在这里:

{
  "compilerOptions": {
    "module": "commonjs",
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "outDir": "lib",
    "sourceMap": true,
    "esModuleInterop": true,
    "strict": true,
    "target": "es2017"
  },
  "compileOnSave": true,
  "include": [
    "src"
  ]
}

您会看到 esModuleInterop 标志设置为 true,这应该可以解决此问题。

我也尝试过这样的导入: import flamelink from 'flamelink/app'; 并得到这个错误:

Module '"/Users/leeprobert/Documents/dev/PebbleStudios/Sciex/Grace/dev/pebble_sciex_grace_react/client/functions/node_modules/flamelink/public"' can only be default-imported using the 'esModuleInterop' flagts(1259)
public.d.ts(61, 1): This module is declared with using 'export =', and can only be used with a default import when using the 'esModuleInterop' flag.

有什么想法吗?

【问题讨论】:

  • 我在导入时刻时遇到了同样的问题。如果您找到解决方案,请更新。

标签: typescript google-cloud-platform google-cloud-functions ecmascript-2017 flamelink-cms


【解决方案1】:

根据警告消息,在我看来,我们需要导入模块的默认值而不是命名空间。你可以试试这个吗?

import flamelink from 'flamelink/app'

【讨论】:

  • 我试过这个。将编辑我的帖子以反映该错误。
  • 该错误表明未打开 esModuleInterop。你能确定 esModuleInterop 是打开的吗?也许 tsconfig 在某处被覆盖。我在这里发现了同样的问题github.com/flamelink/flamelink-js-sdk/issues/58
  • 有趣!通过 CLI 部署的 Cloud Function 可以正常工作,因此我的代码中的错误与 VSCode 的实时编译器有关,而不是与部署过程有关。但是我在全局 tsconfig 中也设置了该标志。所以,不知道如何解决这个错误。
猜你喜欢
  • 2017-05-12
  • 1970-01-01
  • 1970-01-01
  • 2017-10-29
  • 2022-01-14
  • 2018-01-10
  • 1970-01-01
  • 2023-03-20
  • 2017-12-10
相关资源
最近更新 更多