Skip to content

Instantly share code, notes, and snippets.

@oukayuka
Last active May 28, 2020 13:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oukayuka/ca0134e2642d8a5405185fa96a795143 to your computer and use it in GitHub Desktop.
Save oukayuka/ca0134e2642d8a5405185fa96a795143 to your computer and use it in GitHub Desktop.
TypeScript Functions OverLoads Sample
class Brooch {
pentagram = 'Silver Crystal';
}
type Compact = {
silverCrystal: boolean;
};
class CosmicCompact implements Compact {
silverCrystal = true;
cosmicPower = true;
}
class CrisisCompact implements Compact {
silverCrystal = true;
moonChalice = true;
}
function transform(): void;
function transform(item: Brooch): void;
function transform(item: Compact): void;
function transform(item?: Brooch | Compact): void {
if (item instanceof Brooch) {
console.log('Moon crystal power💎, make up!!');
} else if (item instanceof CosmicCompact) {
console.log('Moon cosmic power💖, make up!!!');
} else if (item instanceof CrisisCompact) {
console.log('Moon crisis🏆, make up!');
} else if (!item) {
console.log('Moon prisim power🌙, make up!');
} else {
console.log('Item is fake...😖');
}
}
transform();
transform(new Brooch());
transform(new CosmicCompact());
transform(new CrisisCompact());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment