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

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

问题描述

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

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,这些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 与:

class Other implements ComesFromString {
    constructor (public name: string, count: number) {
    }
}

makeObj(Other); // Error! Other's constructor doesn't match StringConstructable

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

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