javascript - 如何在swagger中使用type(typescript类型)@ApiProperty类型

标签 javascript typescript swagger nestjs nestjs-swagger

我遇到了一个问题。 我需要在我的 API 上公开 swagger 的 @ApiProperty 中的“类型”。 但 Swagger 不接受。我查了很多网站来寻找解决方案,但没有找到任何解决方案。

这是我得到的错误:

TS2693: 'testTest' only refers to a type, but is being used as a value here.

type testTest = 'A' | 'B';

    @ApiProperty({
  type: testTest,
  example: 'fr',
})
test: testTest;

我不能使用其他东西,因为我需要使用的类型来自外部库。

最佳答案

您不能在 Swagger 中使用 TypeScript type 作为 type 的值,因为您从所述库导入的 type 在以下位置不可用运行时,或者一旦它被转换为 JavaScript。它仅用于类型检查。这就是 typescript 的工作原理。这对于任何类型、接口(interface)、返回类型等都是相同的......

我建议你做的是让一个变量保存 type testTest = 'A' | 中的值。 'B';

const testTest = ['A','B'];

@ApiProperty({
  type: String,
  example: testTest[0],
  enum: testTest
})
test: testTest;

关于javascript - 如何在swagger中使用type(typescript类型)@ApiProperty类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71884254/

相关文章:

typescript - 如何强制对函数返回值进行 TypeScript 额外属性检查

Typescript .length 返回未定义

swagger - Swagger UI 中的多级(嵌套)标记

javascript - 为什么我的元素在动画完成后不再隐藏?

Javascript - 如何下载 JSON/AJAX 文件并离线访问它

javascript - 更改此隐藏/展开脚本以实现可访问性

typescript 打字给我 "index.d.ts is not a module"

spring-boot - Springfox @RestController 命名

json - 模型类中 JsonNode 属性的 springfox swagger 配置

javascript - 用于创建模式对话框的 JQuery 编码可以在 jsfiddle 中工作,但不能在本地 html 文件中工作