【问题标题】:TypeScript definition does not match default exportTypeScript 定义与默认导出不匹配
【发布时间】:2019-07-15 14:11:07
【问题描述】:

我正在尝试将 Puppeteer 与 TypeScript、@types/puppeteer 和 Jest 一起使用。

Puppeteer 使用默认导出,它作为 pptr.test.js 工作:

import pptr from 'puppeteer'

pptr.launch(
  // ... config here
)

但是,当我安装 @types/puppeteer 并重命名为 pptr.test.ts 时,我收到此 TS 错误:

模块 '"/home/jack/Documents/Extensions/messages-example/node_modules/@types/puppeteer/index"' 没有默认导出。

代码确实使用 Babel 在 Jest 中转译和运行。

这些都不起作用,但确实通过了 TS 检查:

import { launch } from 'puppeteer'
import * as pptr from 'puppeteer'

两者都因 TypeError 而失败,例如“...不是函数”。

我的 tsconfig.json:

{
  "compilerOptions": {
    "module": "commonjs",
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "outDir": "lib",
    "sourceMap": true,
    "strict": true,
    "target": "es2017",
  }
}

@types/puppeteer 错了吗?处理这种 TypeScript 情况的最佳方法是什么?

【问题讨论】:

    标签: javascript typescript puppeteer typescript-typings


    【解决方案1】:

    您需要在tsconfig.jsoncompilerOptions 属性下设置esModuleInteropallowSyntheticDefaultImports

    您应该可以将其作为默认值导入。

    【讨论】:

    • 尝试,测试。像魅力一样工作!
    猜你喜欢
    • 2018-04-14
    • 2020-04-30
    • 2018-10-14
    • 2022-12-07
    • 2021-06-01
    • 2017-02-01
    • 1970-01-01
    • 2019-11-16
    • 2021-01-09
    相关资源
    最近更新 更多