我有一个curried函数,我需要重载返回的函数签名(简化示例):
const foo = (bar: string) => (tag: string, children?: string[]) => {
const foo = (bar: string) => (tag: string, props: Object, children?: string[]) => {
// Do something
};
重载在使用function
关键字的类方法或函数声明时效果很好,但我无法使用curried函数。
答案 0 :(得分:3)
你可以这样做:
type MyCurriedFunction = {
(tag: string, children?: string[]): void;
(tag: string, props: Object, children?: string[]): void;
}
const foo = (bar: string): MyCurriedFunction => (tag: string, ...args: any[]) => {
// do something
}
foo("str")("tag", ["one", "two"]); // fine
foo("str")("tag", {}, ["one", "two"]); // fine
foo("str")("tag", ["one", "two"], {}); // error