我在解决如何在接口中定义构造函数工作方面遇到了一些麻烦。我可能完全误解了一些事情。但我已经寻找了很长一段时间的答案,我找不到任何与此相关的东西。
我如何在TypeScript类中实现以下接口:
interface MyInterface {
new ( ... ) : MyInterface;
}
Anders Hejlsberg创建了一个包含类似于这个视频(大约14分钟)的界面。但是无论如何我都不能在一个类中实现这个。
我可能误解了什么,我没明白什么?
编辑:
澄清。用“new(…)”我的意思是“任何事”。我的问题是,我甚至不能得到这个工作的最基本的版本:
interface MyInterface {
new () : MyInterface;
}
class test implements MyInterface {
constructor () { }
}
这不是编译对我来说,我得到“类'test'声明接口'MyInterface',但没有实现它:类型'MyInterface'需要一个构造签名,但类型'test'缺乏一个”,当试图编译它。
编辑:
所以在研究了更多的反馈之后。
interface MyInterface {
new () : MyInterface;
}
class test implements MyInterface {
constructor () => test { return this; }
}
不是有效的TypeScript,这并不能解决问题。不能定义构造函数的返回类型。它将返回"test"。以下签名:
类测试{
构造函数(){}
}
似乎是“new () => test”(通过将鼠标悬停在在线编辑器中的“class”上并粘贴该代码获得)。这就是我们想要的,也是我想要的。
谁能提供一个这样的例子或类似的东西,它实际上是编译的?
编辑(…):
所以我可能想到了一个想法,为什么可以在接口中定义这个,但不可能在TypeScript类中实现。以下工作:
var MyClass = (function () {
function MyClass() { }
return MyClass;
})();
interface MyInterface {
new () : MyInterface;
}
var testFunction = (foo: MyInterface) : void => { }
var bar = new MyClass();
testFunction(bar);
那么,这仅仅是TypeScript的一个功能,让你可以接口javascript吗?或者有可能在TypeScript中实现它,而不必使用javascript实现类?
来自官方文件
This is because when a class implements an interface, only the instance side of the class is checked. Since the constructor sits in the static side, it is not included in this check.
Instead, you would need to work with the static side of the class directly. In this example, we define two interfaces, ClockConstructor for the constructor and ClockInterface for the instance methods. Then, for convenience, we define a constructor function createClock that creates instances of the type that is passed to it:
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);
来自官方文件
This is because when a class implements an interface, only the instance side of the class is checked. Since the constructor sits in the static side, it is not included in this check.
Instead, you would need to work with the static side of the class directly. In this example, we define two interfaces, ClockConstructor for the constructor and ClockInterface for the instance methods. Then, for convenience, we define a constructor function createClock that creates instances of the type that is passed to it:
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);