我正在解决如何在接口中定义构造函数的方法。 我可能完全误解某事。 但我已经搜索了一段很好的答案,我找不到与此相关的任何内容。
如何在TypeScript类中实现以下界面:
interface MyInterface { new ( ... ) : MyInterface; }Anders Hejlsberg创建了一个包含与此视频相似的界面(大约14分钟)的界面。 但对于我的生活,我无法在一个班上实现这一点。
我可能会误会某事,我没有得到什么?
编辑:
澄清。 用“新(...)”我的意思是“任何东西”。 我的问题是,我甚至不能得到这个工作的最基本版本:
interface MyInterface { new () : MyInterface; } class test implements MyInterface { constructor () { } }这不是为我编译我得到“类”测试'声明接口'MyInterface',但不实现它:键入'MyInterface'需要一个构造签名,但类型'测试'缺少一个“,当尝试编译它。
编辑:
所以在研究了这一点以后再给予反馈。
interface MyInterface { new () : MyInterface; } class test implements MyInterface { constructor () => test { return this; } }不是有效的TypeScript,这并不能解决问题。 您不能定义构造函数的返回类型。 它将返回“测试”。 以下的签名:class test {constructor(){}}似乎是“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实现类?
I am having some trouble working out how defining constructors in interfaces work. I might be totally misunderstanding something. But I have searched for answers for a good while and I can not find anything related to this.
How do I implement the following interface in a TypeScript class:
interface MyInterface { new ( ... ) : MyInterface; }Anders Hejlsberg creates an interface containing something similar to this in this video (at around 14 minutes). But for the life of me I can not implement this in a class.
I am probably misunderstanding something, what am I not getting?
EDIT:
To clarify. With "new ( ... )" I meant "anything". My problem is that I can not get even the most basic version of this working:
interface MyInterface { new () : MyInterface; } class test implements MyInterface { constructor () { } }This is not compiling for me I get "Class 'test' declares interface 'MyInterface' but does not implement it: Type 'MyInterface' requires a construct signature, but Type 'test' lacks one" when trying to compile it.
EDIT:
So after researching this a bit more given the feedback.
interface MyInterface { new () : MyInterface; } class test implements MyInterface { constructor () => test { return this; } }Is not valid TypeScript and this does not solve the problem. You can not define the return type of the constructor. It will return "test". The signature of the following: class test { constructor () { } } Seems to be "new () => test" (obtained by hovering over "class" in the online editor with just that code pasted in). And this is what we would want and what i thought it would be.
Can anyone provide an example of this or something similar where it is actually compiling?
EDIT (again...):
So I might have come up with an idea as to why it is possible to define this in an interface but not possible to implement in a TypeScript class.The following works:
var MyClass = (function () { function MyClass() { } return MyClass; })(); interface MyInterface { new () : MyInterface; } var testFunction = (foo: MyInterface) : void => { } var bar = new MyClass(); testFunction(bar);So is this only a feature of TypeScript that lets you interface javascript? Or is it possible to implement it in TypeScript without having to implement the class using javascript?
最满意答案
在接口中构造签名在类中是不可实现的; 它们只用于定义现有的JS API来定义“新功能”功能。 这里有一个例子涉及到new签名工作:
interface ComesFromString { name: string; } interface StringConstructable { new(n: string): ComesFromString; } class MadeFromString implements ComesFromString { constructor (public name: string) { console.log('ctor invoked'); } } function makeObj(n: StringConstructable) { return new n('hello!'); } console.log(makeObj(MadeFromString).name);这将为您可以通过makeObj方式调用makeObj创建一个实际的约束:
class Other implements ComesFromString { constructor (public name: string, count: number) { } } makeObj(Other); // Error! Other's constructor doesn't match StringConstructableConstruct signatures in interfaces are not implementable in classes; they're only for defining existing JS APIs that define a 'new'-able function. Here's an example involving interfaces new signatures that does work:
interface ComesFromString { name: string; } interface StringConstructable { new(n: string): ComesFromString; } class MadeFromString implements ComesFromString { constructor (public name: string) { console.log('ctor invoked'); } } function makeObj(n: StringConstructable) { return new n('hello!'); } console.log(makeObj(MadeFromString).name);This creates an actual constraint for what you can invoke makeObj with:
class Other implements ComesFromString { constructor (public name: string, count: number) { } } makeObj(Other); // Error! Other's constructor doesn't match StringConstructable
更多推荐
发布评论