Open3

【JS/TS】Class Transformer (class-transformer) について

まさぴょんまさぴょん

Class Transformerで、ObjectやArrayを紐づける場合

  • Class Transformerで、ObjectやArrayを紐づける場合は@Type(() => xxxxx)を使用する
import { Expose, Type } from "class-transformer";

import { UserType } from "$shared/types/enum/UserType";
import { PostRes } from "$shared/types/res/posts/PostRes";

export class userRes {
  @Expose() userId!: number;
  @Expose() userType!: UserType;
  @Expose() name!: string;
  @Expose() email!: string;
  @Expose() description!: string;
  @Expose() thumbnailUrl!: string;
  // Date Object
  @Type(() => Date)
  @Expose()
  startDate!: Date;
  // 投稿データ Array
  @Type(() => PostRes)
  @Expose()
  posts!: PostRes[];
}