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

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


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)


// I use this workaround:export default class CreateHandler extends BaseHandler {    // final prop used as method    public readonly create = (blueprint: Blueprint): Response<Record> => {        return blueprint.create();    };    // normal method    public store(blueprint: Blueprint): Response<Record> {        return this.response(blueprint.create());    }}