TS2420

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

Bad Code ❌

1
2
3
4
5
6
7
8
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
8
interface Animal {
name: string;
}

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

Video: https://youtu.be/62J_eQsK0e0?t=242