Tip #32[TS override keyword]
TypeScript in version 4.3 introduced the override keyword which allows you to explicitly state that you intend to override a method from a base class.
Let’s assume the following inheritance hierarchy:
If you do not use the override keyword, you may accidentally make a typo in a derived class (e.g. instead of sayHello you may name it saysHello), which can be hard to spot. As a result you will be left with a new method rather that an overridden one. In addition, removing an overridden method from a base class will not result in an error as well. You can easily tackle the aforementioned issues by using the override keyword:
Now, in the both scenarios, you will get the following error:
This member cannot have an ‘override’ modifier because it is not declared in the base class ...
You can also make use of the new option in a tsconfig file, namely the noImplicitOverride which defaults to false. If you set it to true, you will always have to be explicit when overriding a method from a base class, otherwise you will get the following error:
This member must have an ‘override’ modifier because it overrides a member in the base class …
It prevents you from accidentally overriding an already existing method in a base class.
If you like the tip, please give me some applause 👏