Interface Methods in TypeScript

Object interfaces can also add object methods. Let's add a method to our interface that should greet the user. To do this, we must specify the method name, parameter types, and return type:

typescriptinterface IUser {
  name: string;
  age: number;

  greet(text: string): string;
}

Let's implement our method in an object:

typescriptlet user: IUser = {
  name: 'john',
  age: 30,

  greet(text: string): string {
    return text + ', ' + this.name
  }
}

Let's call our method:

typescriptconsole.log(user.greet('hello')); // 'hello, john'
Задание 1

For the IUser interface, implement a method to check the age. If the user's age is less than 18, display a message that access is denied.

Задание 2

Create an interface IMath with properties num1 and num2, and a method getSum that will sum both numbers.