I have a class Manager
:
class Manager{
/**
* Elements that are managed by the manager.
*/
private _elements: PIXI.DisplayObject[];
and a class TextureManager
which extends the Manager
:
export class TextureManager extends Game.Managers.Manager{
private _elements: PIXI.DisplayObjectContainer[];
}
Just so you know, 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)
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'.
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.
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
The TypeScript uses class inheritance through the extends keyword. TypeScript supports only single inheritance and multilevel inheritance. It doesn't support multiple and hybrid inheritance. We can declare a class inheritance as below.
TypeScript supports inheritance like ES6. To inherit a class, you use the extends keyword. For example the following Employee class inherits the Person class: class Employee extends Person { //.. }
We inherit constructor functionality with the super() constructor. If the parent class constructor has any parameters, the super() constructor will need the same parameters.
Classes in TypeScript, like JavaScript, are a special syntax for its prototypical inheritance model, which is comparable to inheritance in class-based object-oriented languages. Classes are just special functions added to ES6 that are meant to mimic the class keyword from these other languages.
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
.
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).
So in this case just use TypeScript 1.3, make _elements
protected and give it a type that it's possible to be overriden.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With