我遇到了一个问题。 我需要在我的 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/