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

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

问题描述

我有一些麻烦工作如何定义构造函数在接口工作。我可能完全误解了一些东西。但我已经搜索了一个好时机的答案,我找不到任何与此相关的。

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?

编辑:

澄清。用new(...)我的意思是任何东西。我的问题是,我不能得到这个工作的最基本的版本:

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

这不是为我编译我得到类'测试'声明接口'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.

编辑:

interface MyInterface {
    new () : MyInterface;
}

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

无效的TypeScript并不能解决问题。您不能定义构造函数的返回类型。它将返回test。签名如下:
class test {
constructor(){}
}
看起来是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的一个特性,或者是否可以在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 的工作示例:

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 with:

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

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

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