#How to dynamically type @ApiProperty({type: ()=>T})

12 messages · Page 1 of 1 (latest)

lunar pawn
#

How can I achieve this

`import { ApiProperty } from "@nestjs/swagger"

export class ApiResponseModel<T>{
status?: string
success: boolean
message: string
error?: boolean
@ApiProperty({ type:() => T })
data: T
}`

It currently throws 'T' only refers to a type, but is being used as a value here.ts(2693)

tall badger
#

You can’t, basically

#

You would have to know exactly all the shapes for that data field

small frigate
#

Well, you technically can, through a function that returns a concrete class definition

tall badger
#

I meant you can do it with types only

lunar pawn
#

Thank you for the feedback. Unfortunately, I'm not able to get it working. Getting this error row new Error(A circular dependency has been detected (property key: "${key}"). Please, make sure that each side of a bidirectional relationships are using lazy resolvers ("type: () => ClassType").);
^
Error: A circular dependency has been detected (property key: "data"). Please, make sure that each side of a bidirectional relationships are using lazy resolvers ("type: () => ClassType").

lunar pawn
#

Somehow, it's working now. This is the final implementation.

#

export const ApiResponseConfig = <TModel extends Type<any>>(model: TModel, status: number) =>{ return applyDecorators( ApiExtraModels(ApiResponseModel, model), ApiResponse({ status: status ?? 200, schema: { allOf: [ { $ref: getSchemaPath(ApiResponseModel) }, { properties: { data: { type: 'array', items: { $ref: getSchemaPath(model) }, }, }, }, ], }, }) )}

#

`export class ApiResponseModel<T>{

// data?: T;

@ApiProperty()
status?: string

@ApiProperty()
success: boolean

@ApiProperty()
message: string

@ApiProperty()
error?: Boolean

}`

#

@ApiResponseConfig(GetWalletModel, 201)