目标是使用方法重载来构建开发人员友好的SDK。serviceType
参数工作正常,但onMessage
参数不工作。TypeScript是否不支持此功能?如果是,是否有解决方法?
type SwitchMessage = {
propA: string;
};
type OutletMessage = {
propB: string;
};
type ServiceType = "Switch" | "Outlet";
type ServiceMessage = SwitchMessage | OutletMessage;
class Client {
subscribe(serviceType: "Switch", onMessage: (msg: SwitchMessage) => void);
subscribe(serviceType: "Outlet", onMessage: (msg: OutletMessage) => void);
subscribe(serviceType: ServiceType, onMessage: (msg: ServiceMessage) => void) {
console.log("Hello")
}
}
来自编译的消息:
This overload signature is not compatible with its implementation signature.
1条答案
按热度按时间pkwftd7m1#
|
处理函数参数的方式与您想象的 * 相反 *Playground