I recently started using TypeScript, but I'm stuck in one place and can't find a solution to one problem. Can you answer? How to shorten the code and get rid of the creation of an empty intermediate class PaginatedUserDTO ?
file: paginated-user.dto.ts
export class PaginatedUserDTO extends Paginated(UserDTO) {}
file: user.service.ts
async find(filter?: findArgs): Promise<PaginatedUserDTO> {
const { first, after, last, before, find } = filter;
const query = this.User.find(find);
return paginate(query, filter);
}
I tried writing something like:
async find(filter?: findArgs): Promise<Type extends Paginated(BusStopDTO)> {
...
}
But it didn't work.
CodePudding user response:
If I understood correctry, Paginated<T>(classRef: Type<T>)
returns Type<IPaginatedType<T>>
, so you may try something like
async find(filter?: findArgs): Promise<Type<IPaginatedType<BusStopDTO>>> {
...
}
Comment me if it didn't work
CodePudding user response:
Thank you! Yes, it works. But I'm still unable to change the parameter of the Query() decorator. It's part of @nestjs/graphql, I'll try to google it for a solution.
file: user.resolve.ts
@Query(() => PaginatedUserDTO)
async find(@Args() filter?: findArgs) : Promise<IPaginatedType<UserDTO>> {
return this.userService.find(filter);
}
file: user.service.ts
async find(filter?: findArgs): Promise<IPaginatedType<UserDTO>> {
const { first, after, last, before, find } = filter;
const query = this.userModel.find(find);
return paginate(query, filter);
}
If I write the following, an error will appear:
file: user.resolve.ts
@Query(() => IPaginatedType<UserDTO>)
async find(@Args() filter?: findArgs) : Promise<IPaginatedType<UserDTO>> {
return this.userService.find(filter);
}