TS2741

error TS2741: Property ‘name’ is missing in type ‘{}’ but required in type ‘Animal’.

Bad Code ❌

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

const laika: Animal = {};

Fixed Code ✔️

Interfaces can be used with classes or plain objects. If we want our object (i.e. laika) to fulfill the contract of Animal, we have to assign all required properties to it:

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

const laika: Animal = {
name: 'Laika',
};

Video: https://www.youtube.com/watch?v=62J_eQsK0e0&t=1155s