[英]Type is missing the following properties from type?
I got this message 我收到了这条消息
Class 'Motorvoertuig' incorrectly implements interface 'Voertuig'. 类'Motorvoertuig'错误地实现了接口'Voertuig'。
Type 'Motorvoertuig' is missing the following properties from type 'Voertuig': toonMerk, wieIsDeEigenaar 类型'Motorvoertuig'缺少类型'Voertuig'的以下属性:toonMerk,wieIsDeEigenaar
But I really don't know why? 但我真的不知道为什么?
I have more code in this document but the error comes from the following Methodes. 我在本文档中有更多代码,但错误来自以下Methodes。
interface Voertuig {
merk: string;
eigenaar: string;
toonMerk(merk:string);
wieIsDeEigenaar(eigenaar:string);
};
abstract class Motorvoertuig implements Voertuig {
(Some code)
}
How to get rid of this error? 如何摆脱这个错误?
You have marked Motorvoertuig
as implementing Voertuig
interface that means you need to add all fields present in Voertuig
interface to Motorvoertuig
class. 您已将Motorvoertuig
标记为实现Voertuig
接口,这意味着您需要将Voertuig
接口中存在的所有字段添加到Motorvoertuig
类。 So for example: 例如:
abstract class Motorvoertuig implements Voertuig {
merk: string = '';
eigenaar: string = '';
toonMerk(merk: string) {}
wieIsDeEigenaar(eigenaar: string) {}
}
or you can try using something like: 或者您可以尝试使用以下内容:
abstract class Motorvoertuig implements Partial<Voertuig> {
}
check advanced typescript types 检查高级打字稿类型
When you implement an interface, you need to define the properties that are declared in the interface. 实现接口时,需要定义接口中声明的属性。 In Motorvoertuig
, you probably have defined merk
and eigenaar
, but not toonMerk
and wieIsDeEigenaar
. 在Motorvoertuig
,你可能已经定义了merk
和eigenaar
,但不是toonMerk
和wieIsDeEigenaar
。
If you don't want to implement those methods inside your abstract class Motorvoertuig
, you can make the methods abstract like this: 如果您不想在抽象类Motorvoertuig
实现这些方法,可以使方法像这样抽象:
abstract toonMerk(merk: string);
abstract wieIsDeEigenaar(merk: string);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.