可以通过在实体上添加 @ApiProperty() 注解来解决此问题,其中指定 DTO 属性并在生成的 TypeScript 接口中使用它们。示例如下:
import { ApiProperty } from '@nestjs/swagger';
export class User {
@ApiProperty()
id: number;
@ApiProperty()
firstName: string;
@ApiProperty()
lastName: string;
}
使用这种方法,生成的 TypeScript 接口将包含指定的属性。
interface User {
id: number;
firstName: string;
lastName: string;
}
这样,我们就可以使用生成的 TypeScript 接口,直接与 API 交互,而不必手动创建 DTO。