简体   繁体   English

了解TypeScript中的特定继承类型

[英]Understanding specific type of inheritance in TypeScript

Could somebody please explain what does this kind of inheritance in TypeScript mean? 有人可以解释一下TypeScript中的这种继承是什么意思吗? There is any chance to create alias for everything what we have after extends keyword? 有机会为extends关键字之后的所有内容创建别名吗?

class MyClass extends (FooClass as { new(): BarClass })

It's important to keep in mind that classes in TypeScript exist both in 'value' space (the realm of objects that exist at run time) and in 'type' space (the realm of concepts that only exist at compile time). 重要的是要记住,TypeScript中的类既存在于“值”空间(在运行时存在的对象的领域)中,又存在于“类型”空间(仅在编译时存在的概念的领域)中。 Consider how this gets compiled into ES5: 考虑一下如何将其编译到ES5中:

class MyClass extends (FooClass as { new(): BarClass }) { }

Becomes: 变为:

var MyClass = /** @class */ (function (_super) {
    __extends(MyClass, _super);
    function MyClass() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    return MyClass;
}(FooClass));

Note that FooClass is passed in as part of the type definition idiom as a reference to the _super class, but BarClass isn't referenced at all (hint: because it's in 'type' space). 请注意, FooClass作为类型定义惯用法的一部分传入,以作为对_super类的引用,但是BarClass没有引用BarClass (提示:因为它在“ type”空间中)。 So the FooClass here references the value of FooClass , but assumes the value of FooClass is of type { new(): BarClass } . 所以FooClass这里引用的价值 FooClass ,但假定值FooClass的类型为{ new(): BarClass }

In other words the type that's used here for the purpose of type checking is just { new(): BarClass } but at run-time, MyClass will actually extend FooClass . 换句话说,这里用于类型检查的类型只是{ new(): BarClass }但是在运行时, MyClass实际上将扩展FooClass I suppose how much difference this distinction really makes depends on the exact definitions of FooClass and BarClass . 我想这种区别的确有多大差异取决于FooClassBarClass的确切定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM