简体   繁体   English

TypeScript 实现接口 class

[英]TypeScript implementing interface in class

I am new in TypeScript, could anyone help me to figure it out the best practices with implementing interface in class?我是 TypeScript 的新手,谁能帮我找出在 class 中实现接口的最佳实践? Because when I tried to following the Docs ( Class Heritage ), I caught in some problem like so:因为当我尝试遵循文档( Class Heritage )时,我遇到了一些问题,如下所示:

  1. declare interface声明接口
interface Notification {
    message: string;
    send(msg?: string): void;
}
  1. implements interface in class with constructor()使用constructor()在 class 中实现接口
class Notifier implements Notification {
    constructor(
        public message: string,
    ){}

    send(customMsg?: string): void {
        if (customMsg) {
            console.log(customMsg);
        } else {
            console.log(this.message);
        }
    }
}
  1. example, using class例如,使用 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'. Class 'Notifier' 错误地实现了接口 'Notification'。 Type 'Notifier' is missing the following properties from type 'Notification': actions, badge, body, data, and 19 more. “通知程序”类型缺少“通知”类型的以下属性:操作、徽章、正文、数据等 19 个。

Could anyone tell me what is my wrong?谁能告诉我我怎么了? Thanks in advance!提前致谢!

Your problem is that the TypeScript standard library already includes a globally-scoped interface named Notification corresponding to the Notification interface from the Web Workers API .您的问题是TypeScript 标准库已经包含一个名为Notification的全局范围interface ,对应于来自Web Workers APINotification接口 Because of this, your Notification interface definition is just merging additional members into it .因此,您的Notification interface定义只是将其他成员合并到其中 This is obviously not what you intend.这显然不是你想要的。

The fix here is either to rename your interface to something else like MyNotification , or create a module or namespace that creates a new naming scope for your code:此处的修复方法是将您的interface重命名为MyNotification类的其他名称,或者创建一个modulenamespace ,为您的代码创建一个新的命名 scope:

// use of "export" makes your code a module
export interface Notification { 
    message: string;
    send(msg?: string): void;
}

or或者

namespace MyNamespace {
  export interface Notification {
    message: string;
    send(msg?: string): void;
  }
}

And then you should be able to refer to it later.然后您应该可以稍后参考它。 If you use the namespace approach, you'll get this:如果你使用namespace方法,你会得到这个:

class Notifier implements MyNamespace.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!");

Which works as desired.哪个可以按需要工作。

Playground link to code Playground 代码链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM