hdnn
hdnn

Reputation: 2147

How to implement an interface which inherits an class in Typescript?

I need to implement some count of interfaces (IUserAccess for example).

All of these interfaces inherit the IBase interface which contains an Execute method, so I want to implement IBase just one time.

Like this:

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({...});
    ...
  }

}

But IDE says:

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

How I can resolve my issue?

Upvotes: 0

Views: 55

Answers (1)

Mamphir
Mamphir

Reputation: 325

Possible solution

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;
  }
}

Upvotes: 2

Related Questions