Created
April 28, 2018 00:18
-
-
Save elyosemite/c17e3ed3171342f86031e039d437d854 to your computer and use it in GitHub Desktop.
This file contains hidden or 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); | |
| } | |
| interface ClockInterface { | |
| tick(); | |
| } | |
| function createClock(ctor: ClockConstructor, hour: number, minute: number): ClockInterface { | |
| return new ctor(hour, minute); | |
| } | |
| class DigitalClock implements ClockInterface { | |
| constructor(h: number, m: number) { } | |
| 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); | |
| let analog = createClock(AnalogClock, 7, 32); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've taken this example of the doc (link bellow).
I didn't understand how this elements (functions and interfaces) relate to each other.
createClock's first parameter is of the typeClockConstructor, but we pass one class that implements other interface's type, that'sClockInterface? I expected, at least, one type that implementClockConstructor. How it works?http://www.typescriptlang.org/docs/handbook/interfaces.html