【发布时间】:2018-10-11 12:49:49
【问题描述】:
我正在尝试在我的应用程序中使用 json-rpc-ws 库,但我遇到了由库定义的处理程序函数签名问题。
该库在声明文件中具有以下类型别名(连接是一个接口):
export type Handler<TConnection extends Connection, ParamType, ParamCallbackType> = (this: TConnection, params: ParamType, reply: ReplyCallback<ParamCallbackType>) => void;
export type ReplyCallback<ParamType> = (error: any, params?: ParamType) => void;
现在我正在尝试通过以下方式在测试代码中创建处理函数:
// Define a function
const func = (conn: Connection, params: IMyRequest, reply: ReplyCallback<IMyResponse> ) => { }
// Try cast to a handler
const handlerFunc = func as Handler<Connection, IMyRequest, IMyResponse>;
但是我收到一个错误,func 无法转换为相应的类型。我在这里缺少什么?
如果有什么不同,我正在使用 Typescript 2.8.1。
【问题讨论】:
-
请提供完整的错误信息。
-
[ts] Type '(conn: Connection, params: IMyRequest, reply: ReplyCallback<IMyResponse>) => void' cannot be converted to type 'Handler<Connection, IMyRequest, IMyResponse>'.
标签: typescript