Created
March 31, 2019 08:18
-
-
Save iamdylanngo/471379b742dedd76436690612a8f1437 to your computer and use it in GitHub Desktop.
This file contains 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
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Type constructor Typescript interface ClockConstructor {new (hour: number, minute: number): ClockInterface;}