Type 'X' is missing the following properties from type 'Y'

avatar

Borislav Hadzhiev

Last updated: Jul 25, 2022

banner

Photo from Unsplash

Type 'X' is missing the following properties from type 'Y' #

The error "Type is missing the following properties from type" occurs when the type we assign to a variable is missing some of the properties the actual type of the variable expects. To solve the error, make sure to specify all of the required properties on the object.

type missing following properties from type

Here are some examples of how the error occurs.

index.ts
type Person = { name: string; country: string; }; // ⛔️ Error: Type '{}' is missing the following // properties from type 'Person': name, country ts(2739) const person: Person = {}; // ------------------------------------------------------ function getPerson(person: Person) { return person; } // ⛔️ Type '{}' is missing the following properties // from type 'Person': name, country ts(2345) getPerson({}); // ------------------------------------------------------ const people: Person[] = []; // ⛔️ Type '{}' is missing the following properties // from type 'Person': name, country ts(2345) people.push({}); // ------------------------------------------------------ interface Employee { id: number; name: string; } // ⛔️ Type 'Developer' is missing the following // properties from type 'Employee': id, name ts(2420) class Developer implements Employee {}

In the first example, we declared the person variable as an empty object, but set its type to be Person, which expects the name and country properties.

index.ts
type Person = { name: string; country: string; }; // ⛔️ Error: Type '{}' is missing the following // properties from type 'Person': name, country ts(2739) const person: Person = {};

For the variable to be of type Person, it has to define all of the required properties of the type.

One way to solve the error is to provide initial values for the name and country properties.

index.ts
type Person = { name: string; country: string; }; const person: Person = { name: '', country: '', };

The person variable now correctly satisfies the Person type, so the error is resolved.

Another way to solve the error is to mark the properties that we don't want to set on the object as optional in the Person type.

index.ts
type Person = { name?: string; // 👈️ marked optional country?: string; // 👈️ marked optional }; const person: Person = {};

We used a question mark to set the name and country properties in the Person type to optional.

This means that they can either be of type string or have an undefined value.

This solves the error because the properties are no longer required, therefore they aren't missing.

If you are calling a function that has an object parameter, make sure to specify all of the properties the object type requires.

index.ts
type Person = { name: string; country: string; }; function getPerson(person: Person) { return person; } // ⛔️ Type '{}' is missing the following properties // from type 'Person': name, country ts(2345) getPerson({});

If you don't want to have to specify all of the properties of the object when calling the function, set a default parameter.

index.ts
type Person = { name: string; country: string; }; function getPerson(person: Person = { name: '', country: '' }) { return person; } getPerson();
If the function gets called without a value for the person parameter, the default object gets used.

If you have a class that implements an interface, make sure to define all properties and methods the interface requires.

index.ts
interface Employee { id: number; name: string; } // ⛔️ Type 'Developer' is missing the following // properties from type 'Employee': id, name ts(2420) class Developer implements Employee {}

The id and name properties are required, so they have to be defined in the class.

index.ts
interface Employee { id: number; name: string; } class Developer implements Employee { constructor(public id: number, public name: string) { this.id = id; this.name = name; } }
The error "Type is missing the following properties from type" sometimes occurs if we define a type or an interface using a name that clashes with a globally defined interface, or one from a third-party module.

If you notice a strange error message with property and method names you don't recognize, try renaming your interface or type alias.

Conclusion #

The error "Type is missing the following properties from type" occurs when the type we assign to a variable is missing some of the properties the actual type of the variable expects. To solve the error, make sure to specify all of the required properties on the object.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.