swagger 将`import`语句添加到`global.d.ts`会破坏类型和模块声明

kognpnkq  于 8个月前  发布在  其他
关注(0)|答案(1)|浏览(55)

在我的Next.js web项目中,我的types/global.d.ts是这样开始的:

declare module 'react-zeroconfig-components'

interface Product {
  ...
}

我使用openapi-typescript从Swagger OpenAPI定义中生成类型,但是当我导入生成的swagger.ts文件时:

import { definitions } from 'types/swagger'

declare module 'react-zeroconfig-components'

interface Product {
  ...
}

... global.d.ts中的其他类型/接口都不再工作了:

Type error: Cannot find name 'Product'.

declare module也停止工作:

Could not find a declaration file for module 'react-zeroconfig-components'

这是怎么回事?

rvpgvaaj

rvpgvaaj1#

使用import使文件成为模块,而不再是环境类型声明文件。要解决此问题,请使用import函数语法:

type SomethingCool = import("types/swagger").definitions;

这是因为如果你导入一些东西并将鼠标悬停在它上面:

import mod from "cool-module";

您可能会看到编辑器(至少是VSCode)将import("cool-module")显示为类型。
所以在这里我们将使用它来减少静态import关键字的使用。

相关问题