使用接口作为招摇模式 - NestJS DTO

Use interface as a swagger schema - NestJS DTO

我有以下代码:

import { IsNotEmpty, IsArray, ArrayMinSize } from 'class-validator';
import { ApiProperty } from '@nestjs/swagger';

export class PublishDto {
  @IsNotEmpty()
  @IsArray()
  @ArrayMinSize(1)
  @ApiProperty({
    type: [Product]
  })
  products: Product[];
}

interface Product {
  id: string;
  title: string;
  sku: string;
  stock: number;
  description: string;
  shortDescription: string;
  imagesUrl: string[];
  price: number;
  department: string;
  category: string;
  brand: string;
  keywords: string[];
  isActive: boolean;
}

我正在尝试将接口 Product 作为 swagger 的架构,但它不起作用,我收到错误消息。 有什么想法吗?

类型 属性 需要一个在运行时可用的值,例如实际的 Class 或 Javascript String 类型。接口仅在编译时存在,因此以这种方式传递它们是无效的。

如果你想手动传递它,你必须将它转换为 Class 否则你可以看看使用 NestJS Swagger 编译器插件,它使用一些很酷的编译时魔法来自动尝试并为你想出其中的一些东西