Skip to content

Instantly share code, notes, and snippets.

@karol-majewski
Created June 14, 2020 20:51
Show Gist options
  • Save karol-majewski/5a004d47eb882b242972d7c389cc24b2 to your computer and use it in GitHub Desktop.
Save karol-majewski/5a004d47eb882b242972d7c389cc24b2 to your computer and use it in GitHub Desktop.
Overloading curried functions in TypeScript
function sum(x: number): (y: number) => number;
function sum(x: number, y: number): number;
function sum(x: number, y?: number): ((y: number) => number) | number {
if (y === undefined) {
return (y: number) => x + y;
}
return x + y;
// This also works:
// switch (y) {
// case undefined:
// return (y: number) => y + x;
// default:
// return x + y;
// }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment