I have this code:
const baseUpstreamImpl = (
client: IClient,
): ProtocolExtension<
{
foobar: (buff: Buffer) => Promise<string>;
}
> => ({
name: 'base upstream impl',
type: 'upstream',
messageCreators: {
foobar: (buff: Buffer) =>
Promise.resolve({
type: 'foobar',
payload: buff,
}),
},
});
This . is the error I get for the foobar function:
[ts]
Type '(buff: Buffer) => Promise<{ type: string; payload: Buffer; }>' is not assignable to type '(...args: any[]) => Promise<IMessage<{ foobar: (buff: Buffer) => Promise<string>; }, "foobar">>'.
Type 'Promise<{ type: string; payload: Buffer; }>' is not assignable to type 'Promise<IMessage<{ foobar: (buff: Buffer) => Promise<string>; }, "foobar">>'.
Type '{ type: string; payload: Buffer; }' is not assignable to type 'IMessage<{ foobar: (buff: Buffer) => Promise<string>; }, "foobar">'.
Types of property 'type' are incompatible.
Type 'string' is not assignable to type '"foobar"'.
baseUpstreamImpl.ts(11, 5): The expected type comes from property 'foobar' which is declared here on type '{ foobar: (...args: any[]) => Promise<IMessage<{ foobar: (buff: Buffer) => Promise<string>; }, "foobar">>; }'
(property) foobar: (...args: any[]) => Promise<IMessage<{
foobar: (buff: Buffer) => Promise<string>;
}, "foobar">>
And here are my types:
type EnforcePromise<T, P = any> = T extends Promise<P> ? T : Promise<T>;
type Promised<T> = {
[P in keyof T]: (...args: InferArgs<T[P]>) => EnforcePromise<InferType<T[P]>>
};
type Filter<T, Cond, U extends keyof T = keyof T> = {
[K in U]: T[K] extends Cond ? K : never
}[U];
type EventKey<T> = Filter<T, (...args: any[]) => any>;
interface IMessage<T, K extends EventKey<T> = EventKey<T>> {
type: K;
payload: InferArgs<T[K]>;
}
export interface ProtocolExtension<T, U, R = {}> {
name: string;
type: 'upstream' | 'downstream';
handlers: Promised<T>;
messageCreators: {
[K in keyof U]: (
...args: any[]
) => Promise<IMessage<U>>
};
}
InferArgsT extends (...args: infer R) => any ? R : neverGeneric type 'ProtocolExtension<T, U, R>' requires between 2 and 3 type arguments.(which is true) could you update your question with a snippet that reproduces your issue ?