My TypeScript interface includes a single function named "send" with two different allowed signatures.
export interface ConnectionContext {
send(data: ConnectionData): void;
send(data: ConnectionData, timeout: number): Promise<ConnectionData>;
}
I am attempting to create an unnamed object that adheres to this interface:
const context: ConnectionContext = {
send: (data: ConnectionData, timeout?: number): void | Promise<ConnectionData> => {
//
}
};
However, I am encountering errors in TypeScript 2.4.1:
Error:(58, 15) TS2322:Type '{ send: (data: ConnectionData, timeout?: number | undefined) => void | Promise<ConnectionData>; }' is not assignable to type 'ConnectionContext'.
Types of property 'send' are incompatible.
Type '(data: ConnectionData, timeout?: number | undefined) => void | Promise<ConnectionData>' is not assignable to type '{ (data: ConnectionData): void; (data: ConnectionData, timeout: number): Promise<ConnectionData>; }'.
Type 'void | Promise<ConnectionData>' is not assignable to type 'Promise<ConnectionData>'.
Type 'void' is not assignable to type 'Promise<ConnectionData>'.
Although I know this can be achieved using a class, I prefer to avoid creating a complete class if possible.