TS2741

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

Broken 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 Tutorial