TypeScript - 更改孩子的属性类型,这可能吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26947176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeScript - Change property type in child, is it possible?
提问by Vadorequest
I have a class Manager
:
我有一堂课Manager
:
class Manager{
/**
* Elements that are managed by the manager.
*/
private _elements: PIXI.DisplayObject[];
and a class TextureManager
which extends the Manager
:
和一个TextureManager
扩展的类Manager
:
export class TextureManager extends Game.Managers.Manager{
private _elements: PIXI.DisplayObjectContainer[];
}
Just so you know, PIXI.DisplayObjectContainer
extends PIXI.DisplayObject
.
正如你所知,PIXI.DisplayObjectContainer
extends PIXI.DisplayObject
。
I believe that because it is the same type (based on the same object: PIXI.DisplayObject
), the type change should be possible. (at least it is in real OOP languages, like Java, if I remember correctly)
我相信,因为它是同一类型(基于同一对象上:PIXI.DisplayObject
),类型的变化应该是可能的。(如果我没记错的话,至少它是真正的 OOP 语言,比如 Java )
But I get this error message during the compilation. How should I do?
但是我在编译过程中收到此错误消息。我应该怎么做?
TextureManager.ts(9,18): error TS2416: Class 'TextureManager' incorrectly extends base class 'Manager':
Types have separate declarations of a private property '_elements'.
TextureManager.ts(9,18):错误 TS2416:“TextureManager”类错误地扩展了基类“Manager”:
类型具有私有属性“_elements”的单独声明。
Solution:
解决方案:
It may seem like a simple solution, but TS 1.3
has just been released like 3 days ago and add support for the -really wanted- protected
attribute. And it seem to work with a protected
attribute it does work fine to change the type, I just set protected _elements: any;
on the Manager
parent class and customize the type as I want in any child protected _elements: Game.Core.Texture;
. Pretty cool.
这似乎是一个简单的解决方案,但 TS1.3
刚刚在 3 天前发布,并添加了对 -really Wanted-protected
属性的支持。它似乎与一个protected
属性一起工作,它可以很好地更改类型,我只是protected _elements: any;
在Manager
父类上设置并根据需要在任何 child 中自定义类型protected _elements: Game.Core.Texture;
。很酷。
I just get red everywhere since my IDE (WebStorm) hasn't released a support for TS 1.3, but by checking Trigger watcher regardless of syntax error
in the File watcher
I was able to make it work. Support coming soon: https://youtrack.jetbrains.com/issue/WEB-14149
我只是得到,因为我的IDE到处红(WebStorm)尚未发布的TS 1.3的支持,但通过检查Trigger watcher regardless of syntax error
在File watcher
我能够使它发挥作用。即将提供支持:https: //youtrack.jetbrains.com/issue/WEB-14149
回答by Kuba Jagoda
You can't override a private class member in TypeScript. Before the version 1.3, which was released a few days ago, there were only 2 accessibility modifiers in TS: private
and public
, so the only solution here would be to use public
.
您不能在 TypeScript 中覆盖私有类成员。在几天前发布的 1.3 版本之前,TS: private
and中只有 2 个可访问性修饰符public
,因此这里唯一的解决方案是使用public
.
It changed in the version 1.3. While private members still cannot be overriden, TS 1.3 introduces the protected
accessibility modifier. It still prevents the property from being accessed outside the class, however it allows members to be accessed in subclasses and, which is more relevant to the question, allows overriding(but type of property that overrides still has to be assignable to the overriden's one).
它在 1.3 版中发生了变化。虽然仍然无法覆盖私有成员,但 TS 1.3 引入了protected
可访问性修饰符。它仍然阻止在类外部访问该属性,但是它允许在子类中访问成员,并且与问题更相关,允许覆盖(但覆盖的属性类型仍然必须可以分配给被覆盖的属性) .
So in this case just use TypeScript 1.3, make _elements
protected and give it a type that it's possible to be overriden.
因此,在这种情况下,只需使用 TypeScript 1.3,设置_elements
protected 并为其指定一个可以覆盖的类型。