Reputation: 797
interface BaseInter {
name: string;
test(): void;
}
abstract class Abs implements BaseInter { }
In TypeScript, compiler complaints that the class incorrectly implements the interface:
name is missing in type Abs.
Here Abs
is an abstract class and so why do we need to implement the interface over there?
Upvotes: 40
Views: 13855
Reputation: 578
If Base
is an interface (as in this answer), there is no error in Derived
. But if Base
is an abstract class, it works as intended.
abstract class Base {
abstract name: string;
abstract test(): void;
}
interface Abstract extends Base { }
abstract class Abstract { }
class Derived extends Abstract {
// ERROR: Non-abstract class 'Derived' does not implement inherited abstract member 'name' from class 'Abstract'.
// ERROR: Non-abstract class 'Derived' does not implement inherited abstract member 'test' from class 'Abstract'.
}
It feels hacky for me, but as for Typescript 4.9.5 it works.
Upvotes: 0
Reputation: 581
Interfaces are used for defining a contract regarding the shape of an object.
Use constructor to pass in properties to the class
interface BaseInter {
name : string;
test(): boolean;
}
abstract class Abs implements BaseInter {
constructor(public name: string) {}
test(): boolean {
throw new Error('Not implemented')
}
}
Upvotes: 0
Reputation: 4227
You can get what you want with a slight trick that defeats the compile-time errors:
interface baseInter {
name : string;
test();
}
interface abs extends baseInter {}
abstract class abs implements baseInter{
}
This trick takes advantage of Typescript's Declaration Merging, and was originally presented here and posted on a related SO question here.
Upvotes: 13
Reputation: 164307
You need to re-write all of the members/methods in the interface and add the abstract
keyword to them, so in your case:
interface baseInter {
name: string;
test();
}
abstract class abs implements baseInter {
abstract name: string;
abstract test();
}
There was a suggestion for it: Missing property declaration in abstract class implementing interfaces but it was declined for this reason:
Although, the convenience of not writing the declaration would be nice, the possible confusion/complexity arising from this change would not warrant it. by examine the declaration, it is not clear which members appear on the type, is it all properties, methods, or properties with call signatures; would they be considered abstract? optional?
Upvotes: 65