Created
June 14, 2020 20:51
-
-
Save karol-majewski/5a004d47eb882b242972d7c389cc24b2 to your computer and use it in GitHub Desktop.
Overloading curried functions in TypeScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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