1

I'm using AsyncApi decorator for my kafka nestjs microservice, but I do not know how to define type of message. This is how decorator looks like:

@AsyncApiSub({
    channel: Topics.MESSAGE_CREATE,
    description: "...",
    message: {
        name: "CreateMessageResult",
        payload: {
            type: () => /* don't know how to write here */
        }
    }
})

message in this decorator is defined:

export interface AsyncOperationOptions extends Omit<AsyncOperationObject, 'message'> {
    message: {
        name: string;
        payload: {
            type: Type<unknown> | Function | [Function] | string;
        };
    };
}

and it is simple if result of method is some DTO. Then just write type: DTO or type: () => DTO. But my method return value is defined as generic class, e.g.:

export class EventResponse<T = any> {
    protected status: Status = Status.OK;

    constructor(
        protected readonly payload: T
    ) {
    }

    getStatus(): Status {
        return this.status;
    }

    getPayload(): T {
        return this.payload;
    }

    toString(): string {
        return JSON.stringify({
            status: this.getStatus(),
            payload: this.getPayload(),

        })
    }
}

Can someone help me, how to use this generic class as type? This doesn't work:

type: EventResponse<Message> //TS2348: Value of type 'typeof EventResponse' is not callable. Did you mean to include 'new'?

type: new EventResponse<Message>(new Message) //TS2322: Type 'EventResponse' is not assignable to type 'string | Function | Type | [Function]'.   Type 'EventResponse' is not assignable to type '[Function]'

type: () => new EventResponse<Message>(new Message) //compiles, but throws runtime error: Error: Token "" does not exist.

michal pavlik
  • 330
  • 3
  • 18

0 Answers0