typestack / class-transformer

Decorator-based transformation, serialization, and deserialization between objects and classes.
MIT License
6.81k stars 500 forks source link

Deprecate `@Type` and provide better `@Nested` #1680

Closed temeddix closed 7 months ago

temeddix commented 7 months ago

Description

The @Type decorator had 2 problems:

This PR introduces a new @Nested decorator, deprecating @Type, with better JSDoc comments and usage syntax.

Before:

class Photo {
  id!: number;
  filename!: string;
  depth!: number;
}

class Pages {
  table!: number;
  contents!: number;
}

class Album {
  id!: number;
  name!: string;
  @Type(() => Photo)
  photos!: Array<Photo>;
  @Type(() => Pages)
  pages!: Pages;
}

After:

class Photo {
  id!: number;
  filename!: string;
  depth!: number;
}

class Pages {
  table!: number;
  contents!: number;
}

class Album {
  id!: number;
  name!: string;
  @Nested(Photo)
  photos!: Array<Photo>;
  @Nested(Pages)
  pages!: Pages;
}

Documentations are all properly updated. Users using VSCode will receive deprecation warning because @Type is now marked with @deprecated, so that they can gradually transition later on.

Checklist

Fixes

fixes #[issue number], fixes #[issue number]

github-actions[bot] commented 6 months ago

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.