函数变量重载

Function variable overloading

我有一个柯里化函数,我需要重载返回的函数签名(简化示例):

const foo = (bar: string) => (tag: string, children?: string[]) => {
const foo = (bar: string) => (tag: string, props: Object, children?: string[]) => {
  // Do something
};

重载非常适合 class 方法或带有 function 关键字的函数声明,但我无法让它与柯里化函数一起使用。

你可以这样做:

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

(code in playground)