我在解决如何在接口中定义构造函数工作方面遇到了一些麻烦。我可能完全误解了一些事情。但我已经寻找了很长一段时间的答案,我找不到任何与此相关的东西。
我如何在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实现类?
好吧,带有构造签名的接口并不意味着要由任何类实现(乍一看,这对于像我这样具有c# /Java背景的人来说可能看起来很奇怪,但请给它一个机会)。这有点不同。
暂时把它想象成一个带有调用签名的接口(就像Java世界中的@FunctionalInterface)。它的目的是描述一个函数类型…种。描述的签名应该由函数对象来满足…但不是任何高级函数或方法。它应该是一个知道如何构造对象的函数,一个在使用new关键字时被调用的函数。
因此,具有构造签名的接口定义了构造函数的签名!类的构造函数应该符合接口中定义的签名(将其视为实现接口的构造函数)。它就像一个工厂!
下面是一段简短的代码片段,试图演示最常见的用法:
interface ClassicInterface { // old school interface like in C#/Java
method1();
...
methodN();
}
interface Factory { //knows how to construct an object
// NOTE: pay attention to the return type
new (myNumberParam: number, myStringParam: string): ClassicInterface
}
class MyImplementation implements ClassicInterface {
// The constructor looks like the signature described in Factory
constructor(num: number, s: string) { } // obviously returns an instance of ClassicInterface
method1() {}
...
methodN() {}
}
class MyOtherImplementation implements ClassicInterface {
// The constructor looks like the signature described in Factory
constructor(n: number, s: string) { } // obviously returns an instance of ClassicInterface
method1() {}
...
methodN() {}
}
// And here is the polymorphism of construction
function instantiateClassicInterface(ctor: Factory, myNumberParam: number, myStringParam: string): ClassicInterface {
return new ctor(myNumberParam, myStringParam);
}
// And this is how we do it
let iWantTheFirstImpl = instantiateClassicInterface(MyImplementation, 3.14, "smile");
let iWantTheSecondImpl = instantiateClassicInterface(MyOtherImplementation, 42, "vafli");