Support override keyword on class methods
Problem
(Update by @RyanCavanuagh) Please see this comment before asking for "any updates", "please add this now", etc.. Comments not meaningfully adding to the discussion will be removed to keep the thread length somewhat reasonable. ------ (NOTE, this is _not_ a duplicate of Issue #1524. The proposal here is more along the lines of the C++ override specifier, which makes much more sense for typescript) An override keyword would be immensely useful in typescript. It would be an optional keyword on any method that overrides a super class method, and similar to the override specifier in C++ would indicate an intent that "_the name+signature of this method should always match the name+signature of a super class method_". This catches a whole range of issues in larger code bases that can otherwise be easily missed. Again similar to C++, _it is not an error to omit the override keyword from an overridden method_. In this case the compiler just acts exactly as it currently does, and skips the extra compile time checks associated with the override keyword. This allows for the more complex untyped javascript scenarios where the derived class override does not exactly match the signature of the base class. Example use [code block] Example compile error conditions [code block] IntelliSense As well as additional compile time validation, the override keyword provides a mechanism for typescript intellisense to easily display and select available super methods, where the intent is to spe
Error Output
error to omit the override keyword from an overridden method_. In this case the compiler just acts exactly as it currently does, and skips the extra compile time checks associated with the override keyword.
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Implement Override Keyword for Class Methods in TypeScript
TypeScript currently lacks a dedicated keyword to explicitly denote methods that override superclass methods, which can lead to subtle bugs in large codebases where method signatures may not match. The absence of this keyword means that developers may inadvertently create methods that do not correctly override superclass methods, leading to runtime errors and unexpected behavior.
Awaiting Verification
Be the first to verify this fix
- 1
Define the Override Keyword Syntax
Introduce the 'override' keyword in TypeScript's syntax for class methods that are intended to override superclass methods. This keyword should be optional and should not change existing behavior if omitted.
typescriptclass Base { method(): void {} } class Derived extends Base { override method(): void {} } - 2
Implement Compile-Time Checks
Modify the TypeScript compiler to perform additional checks when the 'override' keyword is used. Ensure that the method name and signature match the superclass method exactly. If they do not match, emit a compile-time error indicating the mismatch.
typescript// Example of a mismatch class Base { method(param: number): void {} } class Derived extends Base { override method(param: string): void {} } // Error: method signature does not match - 3
Update IntelliSense for Override Methods
Enhance TypeScript's IntelliSense to provide suggestions for superclass methods when the 'override' keyword is used. This will help developers easily identify available methods to override and ensure correct signatures.
typescript// IntelliSense should suggest Base.method() when typing 'override method' in Derived class - 4
Document the Override Keyword Usage
Update the TypeScript documentation to include information about the new 'override' keyword, its purpose, and examples of its usage. Ensure that developers understand how to use it effectively and the benefits it provides.
typescript// Documentation Example: // Use 'override' to ensure method signature matches superclass: // class Derived extends Base { override method(): void {} } - 5
Test the Implementation
Create a suite of tests to validate the behavior of the 'override' keyword. Ensure that both correct usages and incorrect usages are tested to verify that compile-time errors are emitted as expected.
typescript// Test cases for override keyword const testCases = [ { input: 'class Derived extends Base { override method() {} }', expected: 'no error' }, { input: 'class Derived extends Base { override method(param: string) {} }', expected: 'error' } ];
Validation
To confirm the fix worked, create a class that extends another class and use the 'override' keyword on a method. Ensure that the compiler correctly identifies matching and non-matching signatures, and that IntelliSense suggests the appropriate superclass methods. Run the test suite to verify all cases pass.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep