Skip to content

Suggestion: Final keyword for classes and methods #9264

@0815fox

Description

@0815fox

I'd like to have a final keyword on classes and methods, which specifies, that the class / method must not be overridden by any typescript class. Compile-time checks should be done, such that the following examples generate a tsc compiler error.

final class Foo {

}

class Bar extends Foo {

}
// => Bar cannot extend Foo, because it is final.
class Foo {
  final fooIt():void{

  }
}

class Bar {
  fooIt():void {

  }
}
// => Method fooIt of Bar cannot override fooIt of Foo, because it is final.

E.g. I often use following pattern, where I want to urgently avoid fooIt to be overridden:

import Whatever ...

abstract class Foo {
  private ImportantVariable:boolean;

  protected abstract fooIt_inner:Whatever();

  public final fooIt():Whatever() {
    //do somestate change to aprivate member here, which is very crucial for the functionality of every Foo:
    ImportantVariable = true;
    //call the abstract inner functionality:
    return this.fooIt_inner();    
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions