Existing answers explain well:
- Class versus interface.
- multiple inheritance and implementation of several interfaces.
- Interfaces erased by compilation mean that the compiled code is no different from redundant interfaces in the
implements clause.
But some confusion remains. To approach the question from a different angle:
What situation do we need to use?
No. There is no situation where you need to declare redundant interfaces. It reminds me of something like this:
var v = v = v = 66
Yes, that's fine as far as the compiler goes. No, you never need to do this.
Why is this accepted?
It's easy to see why someone (especially someone with a Java background) might be confused by the lack of a warning. After all, Eclipse has been warning me of this for many years (Hello, Serializable !).
Having the same interface named several times in the same class definition is a bit strange. This may help consider an example of a redundant interface that is likely to happen:
interface StringProducer { getString: () => string; } class Parent implements StringProducer { getString = function(): string { return 'x'; } } class Child extends Parent implements StringProducer { getString = function() : string { return 'y'; } } class GrandChild extends Child implements StringProducer { getString = function(): string { return 'z'; } } console.log(new Parent().getString()); console.log(new Child().getString()); console.log(new GrandChild().getString());
You can (freely) think of the GrandChild class as follows:
public class GrandChild implements StringProducer, StringProducer, StringProducer {
since the class implements all its interfaces and their ancestors.
Should the compiler (or linter, perhaps) bark about this? Do I have to force remove the implements clause with Child and GrandChild ?
I think this is largely a matter of preference. For example, when I open GrandChild in my IDE, I can see in this file all the interfaces that the class implements. On the other hand, I might feel that this is just noise, and you want a warning.
The compiler, of course, doesnโt care and doesnโt need to. But I can understand why you might need a warning for this. The question at the end of the day seems to me (to me): "Why is there no tslint rule for redundant interfaces?" This is a reasonable question that I cannot answer. You can always write such a rule and share it with the rest of us.