我希望设计一个可以直接调用并具有重载功能的消息函数.然后,我还希望Message函数下还有其他函数调用,比如Message.confirm().这似乎不容易在类型脚本中实现,要编写类型描述,但在编写实现时似乎很难一下子满足所有类型.
我试图定义一个类,但在调用该类时,它提示必须使用new关键字.我试着直接定义一个函数,但它不能满足函数的性质.这必须一步一步地完成.
例如,让我定义下面的类,它将提示您必须使用new来调用
class message {
message(msg: string): any
message(msg: string, type: string): any
message(msg: string, type?: string){
}
static confirm(){
}
}
message("123")
我对使用以下定义还有另一个问题:
type Message = {
message(msg: string): any
message(msg: string, type: string): any
confirm(): any
}
const message: Message = function message (msg: string, type?: string)
{
}
message.confirm = function(){}
最后一个问题是,我如何定义和实现消息函数?