I run a method that eventually adds a an object to the array where one of the properties extends IServerResponse.
activeRequests: Array<ActiveRequest>;
interface ActiveRequest {
transactionId: string;
resolve: <T extends IServerResponse>(value: T) => void;
reject: (reason: Error) => void;
timer: NodeJS.Timeout;
progress: undefined | ((progress: Progress) => void);
}
// Example request start
export interface GetActiveProjectServerResponse extends IServerResponse {
type: 'response';
cmd: 'otii_get_active_project';
data: {
project_id: number;
}
}
async run(): Promise<GetActiveProjectResponse> {
let serverResponse = await new Promise<GetActiveProjectServerResponse>((resolve, reject) => {
this.connection.push(
this.requestBody,
this.transactionId,
this.maxTime,
resolve as (value: GetActiveProjectServerResponse) => void,
reject
);
});
}
// Example request end
public push<T extends IServerResponse>(
requestBody: any,
transactionId: string,
maxTime: number,
resolve: (value: T) => void,
reject: (reason: Error) => void,
progress?: (progress: Progress) => void
): void {
this.activeRequests.push({
transactionId,
resolve,
reject,
timer,
progress
});
}
public onMessage(event: { data: WebSocket.Data }): void {
...
let req = this.activeRequests.find(request => {
return request.transactionId === transactionId;
});
req.resolve(serverMessage);
}
But I get an error on the line this.activeRequests.push(...):
[ts]
Argument of type '{ transactionId: string; resolve: (value: T) => void; reject: (reason: Error) => void; timer: Timeout; progress: ((progress: number) => void) | undefined; }' is not assignable to parameter of type 'ActiveRequest'.
Types of property 'resolve' are incompatible.
Type '(value: T) => void' is not assignable to type '<T extends IServerResponse>(value: T) => void'.
Types of parameters 'value' and 'value' are incompatible.
Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated.
Type 'IServerResponse' is not assignable to type 'T'.
I don't understand this. Why is T not compatible? I pass around the same resolve function with the same limitation to type.
How can I fix this problem?