noImplicitOverride
noImplicitOverride là compiler option bắt buộc dùng override keyword khi override method.
- Mặc định:
false - Phiên bản thêm vào: 4.3
Giải thích
Bắt buộc phải viết keyword override trước method khi subclass override method của superclass. Điều này giúp phát hiện khi method đang được override bị xóa hoặc đổi tên trong superclass.
Ví dụ, giả sử có class của toggle button (button click để bật tắt lặp lại) như sau:
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 ();// ...}}
Giờ xét subclass ToggleCountButton có thể đếm số lần đã toggle on/off. ToggleCountButton sẽ như sau:
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 ;}}
Giờ giả sử superclass ToggleButton được thay đổi thành "Không cần hai method để toggle on/off! Setter là đủ":
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 ;// ...}}
Khi đó các method enable(), disable() vốn được override trong subclass sẽ trở thành method vô nghĩa.
noImplicitOverride yêu cầu thêm keyword override vào method đang override để kiểm tra xem có method cùng tên trong superclass hay không. Khi có keyword override nhưng không tồn tại method để override sẽ báo lỗi như sau:
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 ;}}