创建一个实现具有重载函数的接口的匿名对象
Creating an anonymous object that implements an interface with overloaded functions
我有一个 TypeScript 接口,它有一个名为 "send" 的函数,该函数重载了两个允许的签名。
export interface ConnectionContext {
send(data: ConnectionData): void;
send(data: ConnectionData, timeout: number): Promise<ConnectionData>;
}
我正在尝试创建一个实现此接口的匿名对象:
const context: ConnectionContext = {
send: (data: ConnectionData, timeout?: number): void | Promise<ConnectionData> => {
//
}
};
但是,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>'.
我知道我可以用 class 做到这一点,但我宁愿不创建一个完整的 class 如果有某种方法可以做到这一点。
搞清楚了 - 如果函数具有重载签名,请将基函数键入 any
。这是违反直觉的,但 TypeScript 目前是这样工作的。
固定码:
export interface ConnectionContext {
send(data: ConnectionData): void;
send(data: ConnectionData, timeout: number): Promise<ConnectionData>;
}
const context: ConnectionContext = {
send: (data: ConnectionData, timeout?: number): any => {}
};
let a = context.send(1) // void
let b = context.send(1, 2) // Promise<ConnectionData>
请参阅 TypeScript Handbook's functions section 的最后一段:
Note that the function pickCard(x): any
piece is not part of the overload list, so it only has two overloads: one that takes an object and one that takes a number. Calling pickCard
with any other parameter types would cause an error.
我有一个 TypeScript 接口,它有一个名为 "send" 的函数,该函数重载了两个允许的签名。
export interface ConnectionContext {
send(data: ConnectionData): void;
send(data: ConnectionData, timeout: number): Promise<ConnectionData>;
}
我正在尝试创建一个实现此接口的匿名对象:
const context: ConnectionContext = {
send: (data: ConnectionData, timeout?: number): void | Promise<ConnectionData> => {
//
}
};
但是,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>'.
我知道我可以用 class 做到这一点,但我宁愿不创建一个完整的 class 如果有某种方法可以做到这一点。
搞清楚了 - 如果函数具有重载签名,请将基函数键入 any
。这是违反直觉的,但 TypeScript 目前是这样工作的。
固定码:
export interface ConnectionContext {
send(data: ConnectionData): void;
send(data: ConnectionData, timeout: number): Promise<ConnectionData>;
}
const context: ConnectionContext = {
send: (data: ConnectionData, timeout?: number): any => {}
};
let a = context.send(1) // void
let b = context.send(1, 2) // Promise<ConnectionData>
请参阅 TypeScript Handbook's functions section 的最后一段:
Note that the
function pickCard(x): any
piece is not part of the overload list, so it only has two overloads: one that takes an object and one that takes a number. CallingpickCard
with any other parameter types would cause an error.