noImplicitOverride
noImplicitOverrideはメソッドオーバーライドにoverrideキーワードを必須にするコンパイラオプションです。
- デフォルト:
false - 追加されたバージョン: 4.3
解説
サブクラスがスーパークラスのメソッドを拡張したときにoverrideのキーワードをメソッドの前に書くことを強制します。これはスーパークラスの拡張しているメソッドが取り除かれたり、名称が変更されたことを検知することに役立ちます。
たとえば、トグルボタン (クリックするとオン、オフを繰り返すボタン) のクラスが次のようになっているとします。
tsclassToggleButton {protected_active : boolean;public constructor() {this._active = false;}public getactive (): boolean {return this._active ;}publicenable (): void {this._active = true;}publicdisable (): void {this._active = false;}publicpush (): void {if (this._active ) {this.disable ();// ...return;}this.enable ();// ...}}
tsclassToggleButton {protected_active : boolean;public constructor() {this._active = false;}public getactive (): boolean {return this._active ;}publicenable (): void {this._active = true;}publicdisable (): void {this._active = false;}publicpush (): void {if (this._active ) {this.disable ();// ...return;}this.enable ();// ...}}
ここで値のオンオフの切り替えを何回したかを数えられるサブクラスToggleCountButtonを考えます。するとToggleCountButtonは次のようになります。
tsclassToggleCountButton extendsToggleButton {private_counter : number;public constructor() {super();this._counter = 0;}publicenable (): void {this._counter ++;this._active = true;}publicdisable (): void {this._counter ++;this._active = false;}public getcounter (): number {return this._counter ;}}
tsclassToggleCountButton extendsToggleButton {private_counter : number;public constructor() {super();this._counter = 0;}publicenable (): void {this._counter ++;this._active = true;}publicdisable (): void {this._counter ++;this._active = false;}public getcounter (): number {return this._counter ;}}
ここでスーパークラスのToggleButtonが「オンオフの切り替えにメソッドはふたつも要らない!セッターで十分だ」と変更されたとします。
tsclassToggleButton {protected_active : boolean;public constructor() {this._active = false;}public getactive (): boolean {return this._active ;}public setactive (active : boolean) {this._active =active ;}publicpush (): void {this._active = !this._active ;// ...}}
tsclassToggleButton {protected_active : boolean;public constructor() {this._active = false;}public getactive (): boolean {return this._active ;}public setactive (active : boolean) {this._active =active ;}publicpush (): void {this._active = !this._active ;// ...}}
するとサブクラスでオーバーライドしたはずのメソッドenable(), disable()が意味のないメソッドとして残ることになります。
noImplicitOverrideはオーバーライドしているメソッドにoverrideキーワードをつけることによってスーパークラスに同名のメソッドがないかを確認させます。overrideキーワードがついているにもかかわらずオーバーライド元となるメソッドが存在しないと次のようなエラーが発生します。
tsclassToggleCountButton extendsToggleButton {private_counter : number;public constructor() {super();this._counter = 0;}public overrideThis member cannot have an 'override' modifier because it is not declared in the base class 'ToggleButton'.4113This member cannot have an 'override' modifier because it is not declared in the base class 'ToggleButton'.(): void { enable this._counter ++;this._active = true;}public overrideThis member cannot have an 'override' modifier because it is not declared in the base class 'ToggleButton'.4113This member cannot have an 'override' modifier because it is not declared in the base class 'ToggleButton'.(): void { disable this._counter ++;this._active = false;}public getcounter (): number {return this._counter ;}}
tsclassToggleCountButton extendsToggleButton {private_counter : number;public constructor() {super();this._counter = 0;}public overrideThis member cannot have an 'override' modifier because it is not declared in the base class 'ToggleButton'.4113This member cannot have an 'override' modifier because it is not declared in the base class 'ToggleButton'.(): void { enable this._counter ++;this._active = true;}public overrideThis member cannot have an 'override' modifier because it is not declared in the base class 'ToggleButton'.4113This member cannot have an 'override' modifier because it is not declared in the base class 'ToggleButton'.(): void { disable this._counter ++;this._active = false;}public getcounter (): number {return this._counter ;}}