The existing answers do a good job of explaining:
- Class vs. interface.
- multiple inheritance vs. implementing multiple interfaces.
- Interfaces being erased by compilation meaning the compiled code is no different with the redundant interfaces in the
implements clause.
Yet some confusion remains. To come at the question from another angle:
which situation we need to use it?
You don't. There is no situation that you need to declare redundant interfaces. It reminds me of something like this:
var v = v = v = 66
Yes, it's fine as far as the compiler goes. No, you don't ever need to do this.
Why is it 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 about this for years (Hello, Serializable!).
Having the same interface named multiple times in a single class definition is a little weird. It might help to consider a redundant interface example that is more likely to actually 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 (loosely) think of the GrandChild class like this:
public class GrandChild implements StringProducer, StringProducer, StringProducer {
since a class implements all its interfaces and those of its ancestors.
Should the compiler (or linter, perhaps) bark about this? Should I be forced to remove the implements clause from Child and GrandChild?
I think this is largely a matter of preference. For example, when I have GrandChild open in my IDE, I might want to see in that file all the interfaces the class implements. On the other hand, I might feel like this is just noise, and want a warning.
The compiler certainly doesn't care, and doesn't need to. But I can see why you might want a lint warning for this. The question at the end of the day seems (to me) to be "Why isn't there a tslint rule for redundant interfaces?". That's a reasonable question, that I can't answer. You could always write such a rule and share it with the rest of us.