Skip to content

Instantly share code, notes, and snippets.

@ragmha
Created November 26, 2018 14:28
Show Gist options
  • Save ragmha/f680077df74e8f7e47b3c486b9222fdc to your computer and use it in GitHub Desktop.
Save ragmha/f680077df74e8f7e47b3c486b9222fdc to your computer and use it in GitHub Desktop.
function reverse(str: string): string;
function reverse<T>(arr: T[]): T[];
function reverse<T>(stringOrArray: string | T[]): string | T[] {
if (typeof stringOrArray === "string") {
return stringOrArray
.split("")
.reverse()
.join("");
}
return stringOrArray.slice().reverse();
}
let str = reverse("Pepperoni");
console.log(`String reversed ${str}`);
let arr = reverse(["lol", "what", "hahhue"]);
console.log(`Arr reversed ${arr}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment