如何实现在Typescript中继承类的接口?

时间:2019-04-09 07:19:47

标签: typescript oop

我需要实现一些接口(例如IUserAccess)。

所有这些接口都继承了包含执行方法的IBase接口,因此我只想实现一次IBase。

赞:

class IBase{
  Execute(data: any): void{
    console.log('Execute');
  };

interface IUserAccess extends IBase {
  CheckPassword(username: string, password: string): boolean;
}

class UserAccess implements IUserAccess{
  CheckPassword(username: string, password: string): boolean {
    console.log('CheckPassword');
    Execute({...});
    ...
  }

}

但是IDE说:

Class 'UserAccess' incorrectly implements interface 'IUserAccess'.
  Property 'Execute' is missing in type 'UserAccess' but required in type 'IUserAccess'.

如何解决我的问题?

1 个答案:

答案 0 :(得分:2)

可能的解决方案

class IBase{
  Execute(data: any): void{
    console.log('Execute');
  };
}

interface IUserAccess {
  CheckPassword(username: string, password: string): boolean;
}

class UserAccess extends IBase implements IUserAccess {
  CheckPassword(username: string, password: string) {
    console.log('CheckPassword');
    this.Execute({...});
    ...
    return true;
  }
}