2017-01-24 18 views
1

我有一個咖喱功能,我需要重載返回函數簽名(簡化的例子):功能的可調超載

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

重載的偉大工程,具有類方法或函數的聲明與function關鍵字,但還沒沒有能力使它與咖喱功能一起工作。

回答

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 

code in playground

+0

這就是聰明! – marvinhagemeister