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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:56:46  来源:igfitidea点击:

TypeScript - Change property type in child, is it possible?

inheritancetypescriptoverriding

提问by Vadorequest

I have a class Manager:

我有一堂课Manager

class Manager{
    /**
     * Elements that are managed by the manager.
     */
    private _elements: PIXI.DisplayObject[];

and a class TextureManagerwhich extends the Manager:

和一个TextureManager扩展的类Manager

export class TextureManager extends Game.Managers.Manager{
    private _elements: PIXI.DisplayObjectContainer[];
}

Just so you know, PIXI.DisplayObjectContainerextends PIXI.DisplayObject.

正如你所知,PIXI.DisplayObjectContainerextends 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.3has just been released like 3 days ago and add support for the -really wanted- protectedattribute. And it seem to work with a protectedattribute it does work fine to change the type, I just set protected _elements: any;on the Managerparent 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 errorin the File watcherI 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 errorFile 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: privateand public, so the only solution here would be to use public.

您不能在 TypeScript 中覆盖私有类成员。在几天前发布的 1.3 版本之前,TS: privateand中只有 2 个可访问性修饰符public,因此这里唯一的解决方案是使用public.

It changed in the version 1.3. While private members still cannot be overriden, TS 1.3 introduces the protectedaccessibility 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 _elementsprotected and give it a type that it's possible to be overriden.

因此,在这种情况下,只需使用 TypeScript 1.3,设置_elementsprotected 并为其指定一个可以覆盖的类型。