noImplicitOverride
noImplicitOverride
はメソッドオーバーライドにoverrideキーワードを必須にするコンパイラオプションです。
- デフォルト:
false
- 追加されたバージョン: 4.3
解説
サブクラスがスーパークラスのメソッドを拡張したときにoverride
のキーワードをメソッドの前に書くことを強制します。これはスーパークラスの拡張しているメソッドが取り除かれたり、名称が変更されたことを検知することに役立ちます。
たとえば、トグルボタン (クリックするとオン、オフを繰り返すボタン) のクラスが次のようになっているとします。
ts
classToggleButton {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 ();// ...}}
ts
classToggleButton {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
は次のようになります。
ts
classToggleCountButton 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 ;}}
ts
classToggleCountButton 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
が「オンオフの切り替えにメソッドはふたつも要らない!セッターで十分だ」と変更されたとします。
ts
classToggleButton {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 ;// ...}}
ts
classToggleButton {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
キーワードがついているにもかかわらずオーバーライド元となるメソッドが存在しないと次のようなエラーが発生します。
ts
classToggleCountButton 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 ;}}
ts
classToggleCountButton 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 ;}}