【发布时间】:2021-07-20 23:35:09
【问题描述】:
我是 TypeScript 的新手,谁能帮我找出在课堂上实现接口的最佳实践?因为当我尝试关注 Docs (Class Heritage) 时,我遇到了这样的问题:
- 声明接口
interface Notification {
message: string;
send(msg?: string): void;
}
- 用
constructor()在类中实现接口
class Notifier implements Notification {
constructor(
public message: string,
){}
send(customMsg?: string): void {
if (customMsg) {
console.log(customMsg);
} else {
console.log(this.message);
}
}
}
- 示例,使用类
const hello = new Notifier("hello");
hello.send();
hello.send("Alert!");
不幸的是,我发现了如下错误消息:
“通知程序”类错误地实现了“通知”接口。 “通知程序”类型缺少“通知”类型的以下属性:操作、徽章、正文、数据等 19 个。
谁能告诉我我的问题是什么?提前致谢!
【问题讨论】:
-
旁白:我正在将您的
message属性从private更改为public,否则您在解决当前错误后会遇到新错误。由于您不是在询问private与public,因此这个问题超出了问题的范围,我们应该使用public来解决问题,就像我在下面的回答一样。 -
是的,非常感谢您的解释
标签: typescript