Код на игровой площадке содержит ошибки для свойства age, и именно так вы можете ввести метод.
interface PersonInterface {
name: string;
age: number;
// type a method
sayHello(): string
// type function property
repeat: (sentence: string) => string
}
class Person implements PersonInterface {
name: string = 'Mary';
foo: any = 'abc';
sayHello() {
return `Hello my name is ${this.name}`
}
repeat = (input) => {
return `${input}`
}
}
Если хотите Чтобы избежать ошибки, вам следует подумать об использовании абстрактного класса. Следует отметить, что вы не можете создать экземпляр абстрактного класса. Документы
abstract class PERSON {
abstract name: string;
abstract age: number;
abstract repeat(sentence: string): string;
}
class Person extends PERSON{
//
// Will error out for not implementing
// properties and methods
//
}
Ошибки исчезнут
class Person extends PERSON {
constructor(public name: string, public age: number) {
super();
}
repeat(input: string) {
return `${input}`;
}
}