This is a linting rule to force marking of method and property overrides.
- Run
npm install
- Run
npm run build
ornpm run watch
- Run
npm run test
This linting rule will enforce the override decorator to be used when a property or method is overriden. Where a decorator is missing, or one is provided when it is not required, a linting error will be displayed.
Acts as an implementation using decorators until microsoft/TypeScript#2000 is implemented.
If you are building for the web you will need to define the override decorator in your code. The implementation can be an empty function and does not need to do anything.
function override(target: Object, propertyKey: string | symbol, descriptor?: any): any | void {
/// do nothing.
}
As we are using a decorator TypeScript will include it in the output, resulting in an empty function being called. Whilst modern JavaScript engines are intelligent enough to ignore these calls, a post-build step can be added to your build process (gulp
, grunt
, ...) to remove the output from your code. For example, the following RegExp could be used.
/__decorate\(\[\s+override(.|\n)+?;/gmi
See the examples inside the test/overrideRule
folder. The decorator is automatically built in with the linting rule within a NodeJS environment, however for production browser environments the decorator will need to be declared on the window, although does not require an implementation.
/**
* Public property should force override decorator for any implementations
*/
@override public baseProperty = "Implementation";
/**
* Public method should force override decorator for any implementations
*/
@override public baseMethodOne() {
/// noop
}
ERROR: 8:9 overrideTest Missing override decorator, properties and methods must be marked
ERROR: 11:9 overrideTest Unnessesary override decorator, does not exist in heritage clause