I am new in TypeScript, could anyone help me to figure it out the best practices with implementing interface in class? Because when I tried to following the Docs (Class Heritage), I caught in some problem like so:
- declare interface
interface Notification {
message: string;
send(msg?: string): void;
}
- implements interface in class with
constructor()
class Notifier implements Notification {
constructor(
public message: string,
){}
send(customMsg?: string): void {
if (customMsg) {
console.log(customMsg);
} else {
console.log(this.message);
}
}
}
- example, using class
const hello = new Notifier("hello");
hello.send();
hello.send("Alert!");
Unfortunately, I found an error message like the following:
Class 'Notifier' incorrectly implements interface 'Notification'. Type 'Notifier' is missing the following properties from type 'Notification': actions, badge, body, data, and 19 more.
Could anyone tell me what is my wrong? Thanks in advance!
messageproperty fromprivatetopublicor you will hit a new error after you resolve the current one. Since you are not asking aboutprivatevspublic, this issue is therefore outside the scope of the question and we should clear things up by usingpublic, as my answer below does.