Overload signatures allow a function to be called with different signatures. This feature can be used nicely to streamline a set of functions that a library offers to consumers. Wouldn't it be nice for a library that contained condenseString public functions and condenseArray to be streamlined so that it just contained a single public condense function? We'll do just this in this section:
- We have a function that removes spaces from a string:
function condenseString(string: string): string {
return string.split(" ").join("");
}
- We have another function that removes spaces from array items:
function condenseArray(array: string[]): string[] {
return array.map(item => item.split(" ").join(""));
}
- We now want to combine these two functions into a single function. We can do this as follows using union types...