I have multiple endpoints that return a response including pagination details. I would like to use one parent type for the pagination and pass in different data-types for the data
param.
I tried the following, but this was not working because the statement in @Type
gives the error 'T' only refers to a type, but is being used as a value here.
, so it expects a value/class and not a type:
export class PaginatedResponseDto<T> {
@Expose()
@ApiProperty()
skip: number;
@Expose()
@ApiProperty()
take: number;
@Expose()
@ApiProperty()
count: number;
@Expose()
@ApiProperty()
@Type(() => T[]) // <- this is not working, because I cannot use `T` here
data: T[];
constructor(data: any) {
Object.assign(this, data);
}
}
I searched and found this, which is basically working, but unfortunately it seems to transform the response twice, which causes issues with dates.
Is there any other way to do this (instead of writing multiple PaginatedResponseDto
-classes?
You can do something along this line...