How to return generic class from endpoint? #1573
-
In the following example, I am getting a compiler error when passing @Get('/:id')
@Description('Get user')
@(Returns(200, ApiResult<UserResponse>).ContentType('application/json')) //here is where I am getting an error
async getUser(@PathParams('id') id: string): Promise<ApiResult<UserResponse>> {
const user = await this.userService.getUser(id);
if (user === undefined) {
return ApiResult.error<UserResponse>(`unable to find user`);
}
return ApiResult.success<UserResponse>(user);
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
@detroitpro I invite you to read the documentation about generics ;) https://tsed.io/docs/model.html#generics Why this notation is incorrect? Returns(200, ApiResult<UserResponse>) Written as it, ApiResult is considered by typescript as an interface and not to a valid parameter (so a valid javascript object after transpilation)! The correct syntax could be: Returns(200, ApiResult).Of(UserResponse) But you have to add extra decorator on ApiResult to propagate the UserResponse schema on this endpoint. Look the docs to found the answers ;) See you |
Beta Was this translation helpful? Give feedback.
@detroitpro I invite you to read the documentation about generics ;)
https://tsed.io/docs/model.html#generics
Why this notation is incorrect?
Written as it, ApiResult is considered by typescript as an interface and not to a valid parameter (so a valid javascript object after transpilation)!
The correct syntax could be:
But you have to add extra decorator on ApiResult to propagate the UserResponse schema on this endpoint. Look the docs to found the answers ;)
See you
Romain