1

I have a curried function where I need to overload the returned function signature (simplified example):

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

Overloading works great with class methods or function declarations with the function keyword, but I haven't been able to get it to work with curried functions.

0

1 Answer 1

3

You can do this:

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)

Sign up to request clarification or add additional context in comments.

1 Comment

That is clever!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.