Skip to content

Instantly share code, notes, and snippets.

@iamdylanngo
Created March 31, 2019 08:18
Show Gist options
  • Save iamdylanngo/471379b742dedd76436690612a8f1437 to your computer and use it in GitHub Desktop.
Save iamdylanngo/471379b742dedd76436690612a8f1437 to your computer and use it in GitHub Desktop.
interface ClockConstructor {
new (hour: number, minute: number): ClockInterface;
}
interface ClockInterface {
tick(): any;
}
function createClock(ctor: ClockConstructor, hour: number, minute: number): ClockInterface {
return new ctor(hour, minute);
}
class DigitalClock implements ClockInterface {
constructor(h: number, m: number) {
console.log();
}
tick() {
console.log("beep beep");
}
}
class AnalogClock implements ClockInterface {
constructor(h: number, m: number) { }
tick() {
console.log("tick tock");
}
}
let digital = createClock(DigitalClock, 12, 17);
digital.tick();
let analog = createClock(AnalogClock, 7, 32);
@iamdylanngo
Copy link
Author

Type constructor Typescript interface ClockConstructor {new (hour: number, minute: number): ClockInterface;}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment