当你操作类和接口的时候,你要知道类是具有两个类型的:静态部分的类型和实例的类型。 你会注意到,当你用构造器签名去定义一个接口并试图定义一个类去实现这个接口时会得到一个错误:interface ClockConstructor { new (hour: number, minute: number): ClockInterface;}interface ClockInterface { tick();}class Clock implements ClockConstructor { currentTime: Date; constructor(h: number, m: number) { }}这里因为当一个类实现了一个接口时,只对其实例部分进行类型检查。 constructor存在于类的静态部分,所以不在检查的范围内。以上就是这个demo内容,有两个地方想不明白:文档提到constructor存在于类的静态部分,但是类的所有方法是存在的原型(prototype)上面的,这里的静态指的是?ClockConstructor里面的new (hour: number, minute: number): ClockInterface是什么意思呢?
1 回答
慕侠2389804
TA贡献1719条经验 获得超6个赞
静态就是指的 class.prototype
new (hour: number, minute: number): ClockInterface是指constructor的类型签名是(hour: number, minute: number),其构造的对象类型签名是ClockInterface(就是指你实例化后的对象的结构类型)
这个原文档有详细的解释,大体意思就是一个class的声明,分为静态和实例的两个方面,虽然constructor是方法,但是它是prototype上的方法,算静态,所以对于它要使用两个interface来描述。
可能说的不对,详细的东西还是多看看文档吧。
添加回答
举报
0/150
提交
取消
