Typescript 重载类型脚本接口
为什么会出现这种错误 我正在尝试用typescript重载,我已经阅读了文档。但是,我无法识别错误。有人要救吗Typescript 重载类型脚本接口,typescript,interface,overloading,Typescript,Interface,Overloading,为什么会出现这种错误 我正在尝试用typescript重载,我已经阅读了文档。但是,我无法识别错误。有人要救吗 export declare type TCallbackResponse<T> = ICallbackResponse<T>; export type ICallbackResponse<T = string> = { (arg1: T, arg2: T, arg3: T): Promise<void>; (arg1
export declare type TCallbackResponse<T> = ICallbackResponse<T>;
export type ICallbackResponse<T = string> = {
(arg1: T, arg2: T, arg3: T): Promise<void>;
(arg1: T, arg2: T): Promise<void>;
}
function handle<T = string>(name: string, call: ICallbackResponse<T>): void {
}
async function message(pattern: string, channel: string, message: string): Promise<void>
async function message(channel: string, message: string): Promise<void> {
}
async function pmessage(pattern: string, channel: string, message: string): Promise<void> {
}
handle('message', message) // Argument of type '(pattern: string, channel: string, message: string) => Promise<void>' is not assignable to parameter of type 'ICallbackResponse<string>'.(2345)
handle('message', pmessage) // Argument of type '(pattern: string, channel: string, message: string) => Promise<void>' is not assignable to parameter of type 'ICallbackResponse<string>'.(2345)
导出声明类型TCallbackResponse=ICallbackResponse;
导出类型ICallbackResponse={
(arg1:T,arg2:T,arg3:T):承诺;
(arg1:T,arg2:T):承诺;
}
函数句柄(名称:string,调用:ICallbackResponse):void{
}
异步函数消息(模式:string,通道:string,消息:string):承诺
异步函数消息(通道:字符串,消息:字符串):承诺{
}
异步函数pmessage(模式:string,通道:string,消息:string):Promise{
}
handle('message',message)//类型的参数('pattern:string,channel:string,message:string)=>Promise'不可分配给'ICallbackResponse'类型的参数。(2345)
handle('message',pmessage)//类型为“(模式:string,通道:string,消息:string)=>Promise”的参数不能分配给类型为“ICallbackResponse”的参数。(2345)
链接到代码:在您的案例中,以下两种方法称为实现签名而不是重载签名-
async function message(channel: string, message: string): Promise<void> {
}
async function pmessage(pattern: string, channel: string, message: string): Promise<void> {
}
异步函数消息(通道:字符串,消息:字符串):承诺{
}
异步函数pmessage(模式:string,通道:string,消息:string):Promise{
}
从文件-
从外部看不到实现的签名。
编写重载函数时,应该始终有两个或多个
以上签名实现了更多的功能
由于您有多个呼叫签名,并且并非所有呼叫都与(arg1:T,arg2:T):承诺相匹配代码>观察缺失的第三个参数
另一方面,通过仔细查看您的案例,您甚至不需要(arg1:T,arg2:T):承诺
输入ICallbackResponse
此代码示例中有太多无关的内容,因此它不是一个好的示例。未使用的TCallbackResponse
声明的目的是什么?泛型T
类型参数的用途是什么,这些参数仅指定为其默认字符串
类型?你的问题和我的基本相同吗?然后,我不太清楚为什么要这样重载函数;为什么不写下承诺呢?handle()
将如何处理call
?因为回调包含其他参数。在消息函数中,她需要返回两个参数,channel:string,message:string
,另外三个参数,pattern:string,channel:string,message:string
对不起;我不明白。也许有语言障碍?请最好用代码演示一种情况,即(arg1:string,arg2:string,arg3?:string)=>Promise
不优于ICallbackResponse
。而且,你似乎没有回答我的大部分问题。你能不能请你的问题包括一个真正的问题?