TS2420

error TS2420: Class ‘Dog’ incorrectly implements interface ‘Animal’. Property ‘name’ is private in type ‘Dog’ but not in type ‘Animal’.

Broken Code ❌

1
2
3
4
5
6
7
interface Animal {
name: string;
}

class Dog implements Animal {
constructor(private name: string = 'Bobby') {}
}

Fixed Code ✔️

The Animal interface defines a public name member. The name property in our Dog class must therefore also be public:

1
2
3
4
5
6
7
interface Animal {
name: string;
}

class Dog implements Animal {
constructor(public name: string = 'Bobby') {}
}

Video Tutorial