TS2715

error TS2715: Abstract property ‘name’ in class ‘Animal’ cannot be accessed in the constructor.

Bad Code ❌

1
2
3
4
5
6
7
8
9
10
abstract class Animal {
abstract name: string;
}

class Dog extends Animal {
constructor(name: string) {
super();
this.name = name;
}
}

Fixed Code ✔️

The name member of the abstract Animal class is abstract, so we have to define it ourselves in the derived class Dog. Because name has no access modifier, it is public by default which means that our Dog class has to implement it with a public visibility:

1
2
3
4
5
6
7
8
9
10
11
12
abstract class Animal {
abstract name: string;
}

class Dog extends Animal {
public name;

constructor(name: string) {
super();
this.name = name;
}
}