Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an equivalent to "sealed" or "final" in TypeScript?

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().

like image 995
bubbleking Avatar asked Mar 15 '17 15:03

bubbleking


People also ask

What is protected in TypeScript?

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.

How do I override a TypeScript method?

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.

Can I instantiate a sealed class?

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.


2 Answers

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:

  • github.com/Microsoft/TypeScript/issues/9264, and
  • github.com/Microsoft/TypeScript/issues/8306
like image 181
bubbleking Avatar answered Sep 21 '22 19:09

bubbleking


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) 
like image 36
Asher Garland Avatar answered Sep 20 '22 19:09

Asher Garland