TypeScript 定义与默认导出不匹配

Jac*_*eam 2 javascript typescript typescript-typings puppeteer

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

Puppeteer 使用默认导出,其工作原理如下pptr.test.js

import pptr from 'puppeteer'

pptr.launch(
  // ... config here
)
Run Code Online (Sandbox Code Playgroud)

但是,当我安装@types/puppeteer并重命名为时pptr.test.ts,出现以下 TS 错误:

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

该代码使用 Babel 在 Jest 中进行转换和运行。

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

import { launch } from 'puppeteer'
Run Code Online (Sandbox Code Playgroud)
import * as pptr from 'puppeteer'
Run Code Online (Sandbox Code Playgroud)

两者都会失败,并出现类似“...不是函数”的类型错误。

我的 tsconfig.json:

{
  "compilerOptions": {
    "module": "commonjs",
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "outDir": "lib",
    "sourceMap": true,
    "strict": true,
    "target": "es2017",
  }
}
Run Code Online (Sandbox Code Playgroud)

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

Vit*_*kov 5

您需要在您的道具下esModuleInterop设置allowSyntheticDefaultImportscompilerOptionstsconfig.json

然后您应该能够将其导入为默认值。