接口·续1

interface ClockConstructor {  new (hour: number, minute: number);}class Clock implements ClockConstructor {  currentTime: Date;  constructor(h: number, m: number) {}}

这里因为当一个类实现了一个接口时,只对其实例部分进行类型检查。 constructor 存在于类的静态部分,所以不在检查的范围内。

因此,我们应该直接操作类的静态部分。 看下面的例子,我们定义了两个接口,ClockConstructor为构造函数所用和ClockInterface为实例方法所用。 为了方便我们定义一个构造函数createClock,它用传入的类型创建实例。

interface ClockConstructor {  new (hour: number, minute: number): ClockInterface;}interface ClockInterface {  tick(): void;}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);

因为createClock的第一个参数是ClockConstructor类型,在createClock(AnalogClock, 7, 32)里,会检查AnalogClock是否符合构造函数签名。

另一种简单方式是使用类表达式:

interface ClockConstructor {  new (hour: number, minute: number);}interface ClockInterface {  tick();}const Clock: ClockConstructor = class Clock implements ClockInterface {  constructor(h: number, m: number) {}  tick() {    console.log("beep beep");  }};

继承接口

和类一样,接口也可以相互继承。 这让我们能够从一个接口里复制成员到另一个接口里,可以更灵活地将接口分割到可重用的模块里。

interface Shape {  color: string;}interface Square extends Shape {  sideLength: number;}let square = {} as Square;square.color = "blue";square.sideLength = 10;

一个接口可以继承多个接口,创建出多个接口的合成接口。

interface Shape {  color: string;}interface PenStroke {  penWidth: number;}interface Square extends Shape, PenStroke {  sideLength: number;}let square = {} as Square;square.color = "blue";square.sideLength = 10;square.penWidth = 5.0;

混合类型

先前我们提过,接口能够描述 JavaScript 里丰富的类型。 因为 JavaScript 其动态灵活的特点,有时你会希望一个对象可以同时具有上面提到的多种类型。

一个例子就是,一个对象可以同时作为函数和对象使用,并带有额外的属性。

interface Counter {  (start: number): string;  interval: number;  reset(): void;}function getCounter(): Counter {  let counter = function(start: number) {} as Counter;  counter.interval = 123;  counter.reset = function() {};  return counter;}let c = getCounter();c(10);c.reset();c.interval = 5.0;

在使用 JavaScript 第三方库的时候,你可能需要像上面那样去完整地定义类型。

接口继承类

当接口继承了一个类类型时,它会继承类的成员但不包括其实现。 就好像接口声明了所有类中存在的成员,但并没有提供具体实现一样。 接口同样会继承到类的 private 和 protected 成员。 这意味着当你创建了一个接口继承了一个拥有私有或受保护的成员的类时,这个接口类型只能被这个类或其子类所实现(implement)。

当你有一个庞大的继承结构时这很有用,但要指出的是你的代码只在子类拥有特定属性时起作用。 除了继承自基类,子类之间不必相关联。 例:

class Control {  private state: any;}interface SelectableControl extends Control {  sel​ect(): void;}class Button extends Control implements SelectableControl {  sel​ect() {}}class TextBox extends Control {  sel​ect() {}}class ImageControl implements SelectableControl {// Error: Class 'ImageControl' incorrectly implements interface 'SelectableControl'.//  Types have separate declarations of a private property 'state'.  private state: any;  sel​ect() {}}

在上面的例子里,SelectableControl包含了Control的所有成员,包括私有成员state。 因为state是私有成员,所以只能够是Control的子类们才能实现SelectableControl接口。 因为只有Control的子类才能够拥有一个声明于Control的私有成员state,这对私有成员的兼容性是必需的。

在Control类内部,是允许通过SelectableControl的实例来访问私有成员state的。 实际上,SelectableControl就像Control一样,并拥有一个select方法。 Button和TextBox类是SelectableControl的子类(因为它们都继承自Control并有select方法)。而对于 ImageControl 类,它有自身的私有成员 state 而不是通过继承 Control 得来的,所以它不可以实现 SelectableControl 。