interface ClockConstructor {
new (hour: number, minute: number): ClockInterface;
}
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);
这里的
interface ClockConstructor {
new (hour: number, minute: number): ClockInterface;
}
interface ClockInterface {
tick();
}
这几行谁能帮我解答么 ??
原文地址:https://typescript.bootcss.co...
没有任何的特殊之处。Interface 可以定义所有对象类型的接口,这只是其中一种。
这个接口的意思就是:
实现了一个类接口,可以检查 new 关键字的调用方法类型。
同时如果是一个函数,则只能用 new 关键字调用,不能直接调用。【同样的还有其他的定义】:
定义函数接口,实现了 callable,但是不能用 new 关键字。
实现了类接口。但是不会检查 constructor ,因为这属于类静态部分。