You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, TypeScript enforces the override keyword only for class inheritance, ensuring that a method marked as override actually exists in a parent class. However, this behavior does not extend to methods implementing an interface.
I propose that TypeScript should enforce override for both:
Methods that override a superclass method (which is already implemented).
Methods that implement an interface method.
Why?
In Java, the @OverRide annotation applies to both superclass and interface methods, helping catch implementation errors.
Interfaces serve as contracts for a class, and enforcing override would prevent accidental mismatches or missing methods.
While replacing interfaces with abstract classes could achieve this, that is not the point—I prefer a Java-like approach where interfaces define contracts, and override ensures adherence.
Expected Behavior:
If a class method is marked with override, TypeScript should raise a compile-time error if:
The method does not exist in a superclass or any implemented interface.
The method's signature does not match the interface definition.
Current Behavior:
The override keyword is only validated against a superclass, not interfaces.
No enforcement exists for methods that implement an interface but do not match the expected signature.
📃 Motivating Example
interface Animal {
makeSound(): void;
}
class Dog implements Animal {
override makeSound() { // ❌ Expected error: 'makeSound' does not exist in the interface
console.log("Woof!");
}
override eat() { // ❌ Expected error: 'eat' is not in Animal or a superclass
console.log("Eating...");
}
}
💻 Use Cases
Enforce Consistency Between interfaces and their implementations
The text was updated successfully, but these errors were encountered:
🔍 Search Terms
override keyword
interfaces
implements
TypeScript override
method override
interface enforcement
override in interface
Java-style override
missing method in interface
abstract class vs interface
TypeScript type checking
compile-time error
✅ Viability Checklist
⭐ Suggestion
Currently, TypeScript enforces the override keyword only for class inheritance, ensuring that a method marked as override actually exists in a parent class. However, this behavior does not extend to methods implementing an interface.
I propose that TypeScript should enforce override for both:
Methods that override a superclass method (which is already implemented).
Methods that implement an interface method.
Why?
In Java, the @OverRide annotation applies to both superclass and interface methods, helping catch implementation errors.
Interfaces serve as contracts for a class, and enforcing override would prevent accidental mismatches or missing methods.
While replacing interfaces with abstract classes could achieve this, that is not the point—I prefer a Java-like approach where interfaces define contracts, and override ensures adherence.
Expected Behavior:
If a class method is marked with override, TypeScript should raise a compile-time error if:
The method does not exist in a superclass or any implemented interface.
The method's signature does not match the interface definition.
Current Behavior:
The override keyword is only validated against a superclass, not interfaces.
No enforcement exists for methods that implement an interface but do not match the expected signature.
📃 Motivating Example
interface Animal {
makeSound(): void;
}
class Dog implements Animal {
override makeSound() { // ❌ Expected error: 'makeSound' does not exist in the interface
console.log("Woof!");
}
}
💻 Use Cases
Enforce Consistency Between interfaces and their implementations
The text was updated successfully, but these errors were encountered: