具有构造签名的接口如何工作? [英] How does interfaces with construct signatures work?

查看:21
本文介绍了具有构造签名的接口如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在弄清楚接口中定义构造函数的工作方式时遇到了一些麻烦.我可能完全误解了一些东西.但是我已经搜索了很长时间的答案,但找不到与此相关的任何内容.

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.

如何在 TypeScript 类中实现以下接口:

How do I implement the following interface in a TypeScript class:

interface MyInterface {
    new ( ... ) : MyInterface;
}

Anders Hejlsberg 在此视频中创建了一个包含类似内容的界面(大约 14 分钟).但是对于我的一生,我无法在课堂上实现这一点.

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?

澄清一下.新(...)"我的意思是任何东西".我的问题是我什至无法获得此工作的最基本版本:

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 () { }
}

这不是为我编译我在尝试编译时得到类'test'声明接口'MyInterface'但没有实现它:类型'MyInterface'需要一个构造签名,但类型'test'缺少一个".

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.

因此,根据反馈,在对此进行了更多研究之后.

So after researching this a bit more given the feedback.

interface MyInterface {
    new () : MyInterface;
}

class test implements MyInterface {
    constructor () => test { return this; }
}

不是有效的 TypeScript,这不能解决问题.您不能定义构造函数的返回类型.它将返回测试".签名如下:类测试{构造函数 () { }}似乎是new () => test"(通过将鼠标悬停在仅粘贴该代码的在线编辑器中的class"上获得).这就是我们想要的,也是我认为的.

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?

编辑(再次...):

所以我可能想出了一个想法,为什么可以在接口中定义它,但不能在 TypeScript 类中实现.以下工作:

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);

那么这只是 TypeScript 的一项功能,可以让您与 javascript 交互吗?或者是否可以在 TypeScript 中实现它而不必使用 javascript 实现类?

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 签名的例子,它确实有效:

Construct 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);

这为您可以调用 makeObj 的内容创建了一个实际约束:

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

这篇关于具有构造签名的接口如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆