Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforce override keyword for methods implementing an interface #61080

Open
6 tasks done
Muhamedkaric opened this issue Jan 30, 2025 · 1 comment
Open
6 tasks done

Enforce override keyword for methods implementing an interface #61080

Muhamedkaric opened this issue Jan 30, 2025 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@Muhamedkaric
Copy link

🔍 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!");
}

override eat() { // ❌ Expected error: 'eat' is not in Animal or a superclass
    console.log("Eating...");
}

}

💻 Use Cases

Enforce Consistency Between interfaces and their implementations

@MartinJohns
Copy link
Contributor

MartinJohns commented Jan 30, 2025

Sounds like a duplicate of #45136. Used search terms: override interface in:title

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jan 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants