annotate method override in typescript, to make easy in code reviews

Viewed 1700

Is there any naming convention, or snippet editing, or any VS code extension or any NPM libraries to tag to tag override methods in typescript. It is useful in doing code review.

Just like override syntax in Java

@override << ###### SOMETHING like this ######
@public String toString(){
   // your overridden code.
   return something;
}
2 Answers

The override keyword was added in TypeScript 4.3 as you can see here.
Example of use:

class SomeComponent {
  show() {
    // ...
  }
  hide() {
    // ...
  }
}

class SpecializedComponent extends SomeComponent {
  override show() {
    // ...
  }
  override hide() {
    // ...
  }
}

To make it mandatory you can set noImplicitOverride true in your tsconfig.

As of now there is no override keyword or decorator, but you can write your own decorator:

const override = < Sup >( sup : { prototype : Sup } ) => <
    Field extends keyof Sup ,
    Proto extends { [ key in Field ] : Sup[ Field ] } ,
>(
    proto : Proto ,
    field : Field ,
    descr : TypedPropertyDescriptor< Sup[ Field ] > ,
)=> {}

And use it like this:

class Super {
   myMethod():void {
      console.log("Super")
  }
}

class A extends Super {
  @override(Super)
  myMethod():void {
    console.log("A")
  }
}

As taken from github.

Related