有一个非常相似的问题,但是文档中提到的静态端和实例端没有直接答案。
正如我依稀理解的那样,静态端是构造函数,实例端是其他一切?
打字稿文档中的一个示例:
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 …Run Code Online (Sandbox Code Playgroud) 我正在阅读Typescript手册的类类型部分,我对如何编写类的接口定义感到困惑.从文档中,我了解一个接口可以用来描述Class的"实例"方面.但是,您如何编写描述类的"静态"一面的界面?
这是一个例子:
interface IPerson {
name: string;
getName(): string;
}
class Person implements IPerson {
public name: string;
constructor(name: string) {
this.name = name;
}
public getName() {
return this.name;
}
}
Run Code Online (Sandbox Code Playgroud)
在这个例子中,你将如何修改,IPerson以便也可以描述构造函数?