I'm trying to implement a method in a super class that should be available for use, but not changeable, in sub classes. Consider this:
export abstract class BaseClass { universalBehavior(): void { doStuff(); // Do some universal stuff the same way in all sub classes specializedBehavior(); // Delegate specialized stuff to sub classes } protected abstract specializedBehavior(): void; }
My intention would be that any sub class of BaseClass would not only be free to omit implementation of universalBehavior()
, but not even be allowed to provide an implementation. Is this not (yet) possible in TypeScript? Intellisense complains when I omit the implementation in my sub classes. The best I can seem to do is this:
export class SubClass extends BaseClass { universalBehavior(): void { super.universalBehavior(); } specializedBehavior(): void { // sub class' implementation } }
Obviously this is problematic because I have to ensure that no sub class ever implements universalBehavior()
with anything other than a call to super.universalBehavior()
.
protected implies that the method or property is accessible only internally within the class or any class that extends it but not externally. Finally, readonly will cause the TypeScript compiler to throw an error if the value of the property is changed after its initial assignment in the class constructor.
To override a class method in TypeScript, extend from the parent class and define a method with the same name. Note that the types of the parameters and the return type of the method have to be compatible with the parent's implementation. Copied! class Parent { doMath(a: number, b: number): number { console.
Sealed classes cannot be instantiated directly. Sealed classes cannot have public constructors (The constructors are private by default). Sealed classes can have subclasses, but they must either be in the same file or nested inside of the sealed class declaration.
No, at the time of this writing there is not. There is a proposal for such a keyword which is still being considered, but may or may not ever be implemented.
See:
Example of implementation hack of 'sealed method' as readonly property of type function which throws compiler error when attempting to override in extended class:
abstract class BaseClass { protected element: JQuery<HTMLElement>; constructor(element: JQuery<HTMLElement>) { this.element = element; } readonly public dispose = (): void => { this.element.remove(); } } class MyClass extends BaseClass { constructor(element: JQuery<HTMLElement>) { super(element); } public dispose(): void { } // Compiler error: "Property 'dispose' in type 'MyClass' is not assignable to the same property in base type 'BaseClass'" }
TypeScript 2.0 supports "final" classes through using of private constructor:
class A { private constructor(){} } class B extends A{} //Cannot extend a class 'A'. Class constructor is marked as private.ts(2675)
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