Type 'X' has no properties in common with type 'Y' in TS

avatar
Borislav Hadzhiev

Last updated: Feb 28, 2024
4 min

banner

# Type 'X' has no properties in common with type 'Y' in TS

The error "Type 'X' has no properties with type 'Y'" occurs when we try to assign anything to a weak type when there's no overlap in properties.

To solve the error, declare any overlapping properties if they exist or use a type assertion.

type has no properties in common with type

Here is an example of how the error occurs.

index.ts
// ๐Ÿ‘‡๏ธ Weak type (all properties optional) interface Employee { id?: number; salary?: number; } function getEmployee(emp: Employee) { return emp; } // ๐Ÿ‘‡๏ธ No overlap in properties with Employee const emp = { name: 'Bobby Hadz', }; // โ›”๏ธ Error: Type '{ name: string; }' has no // properties in common with type 'Employee'.ts(2559) getEmployee(emp);

type has no properties in common with type in typescript

The Employee type is a weak type, because all of its properties are optional.

The question mark is used to mark the type's properties as optional and means that the property can either be of the specified type or be undefined.

The Employee type is a weak type because you could create an empty object that conforms to the type.

index.ts
interface Employee { id?: number; salary?: number; } // โœ… This is ok const e: Employee = {};
The code for this article is available on GitHub

The cause of the error is that we pass the emp object to a function that expects an object of type Employee and the emp variable has no overlap with the Employee type.

# Use a type assertion to solve the error

One way to solve the error is to use a type assertion.

index.ts
interface Employee { id?: number; salary?: number; } function getEmployee(emp: Employee) { return emp; } const emp = { name: 'Bobby Hadz', }; // ๐Ÿ‘‡๏ธ {name: 'Bobby Hadz'} getEmployee(emp as Employee); // ๐Ÿ‘ˆ๏ธ use type assertion

use type assertion to solve the error

With the type assertion, we effectively tell TypeScript that we know better and the emp variable is definitely of type Employee.

# Make sure there is an overlap between the type and the object

An alternative solution is to make sure there is an overlap between the Employee type and the emp object.

For example, we can add the name property to the Employee type.

index.ts
interface Employee { id?: number; salary?: number; name?: string; // ๐Ÿ‘ˆ๏ธ added name } function getEmployee(emp: Employee) { return emp; } const emp = { name: 'Bobby Hadz', }; // ๐Ÿ‘‡๏ธ {name: 'Bobby Hadz'} console.log(getEmployee(emp));

make sure overlap between type and object

The code for this article is available on GitHub

We added the name property to the Employee type, so now there is an overlap between the type and the emp object.

This solves the error because there is an overlap in properties.

If you aren't able to alter the type, you can add an overlapping property to the object.

index.ts
interface Employee { id?: number; salary?: number; } function getEmployee(emp: Employee) { return emp; } const emp = { name: 'Bobby Hadz', id: 0, // ๐Ÿ‘ˆ๏ธ added id property }; // ๐Ÿ‘‡๏ธ {name: 'Bobby Hadz'} console.log(getEmployee(emp));

We added the id property to the emp object, so there is an overlap between the Employee type and the object, which also solves the error.

# Use an index signature to solve the error

The error can also be solved by using an index signature.

index.ts
interface Employee { id?: number; salary?: number; [key: string]: any; // ๐Ÿ‘ˆ๏ธ added index signature } function getEmployee(emp: Employee) { return emp; } const emp = { name: 'Bobby Hadz', }; // ๐Ÿ‘‡๏ธ {name: 'Bobby Hadz'} console.log(getEmployee(emp));
The code for this article is available on GitHub

The {[key: string]: any} syntax is an index signature in TypeScript and is used when we don't know all the names of a type's properties and the shape of the values ahead of time.

The index signature in the example means that when an object of type Employee is indexed with a string, it will return a value of any type.

You don't have to use any as the type, you can be much more specific for better type safety. However, note that the type of the index signature has to be a union type of all of the possible types in the interface.

index.ts
interface Employee { id?: number; salary?: number; // ๐Ÿ‘‡๏ธ index signature with better type safety [key: string]: string | number | undefined; } const e: Employee = {}; function getEmployee(emp: Employee) { return emp; } const emp = { name: 'James', }; // ๐Ÿ‘‡๏ธ {name: 'James'} console.log(getEmployee(emp));
The code for this article is available on GitHub

We specified that when an object of type Employee is indexed with a string key, it will return a value of type string, number or undefined.

This is necessary, because our id and salary properties have values of type number or undefined (because they are optional), and the id and salary are also string keys.

This approach resolves the error because now there is an overlap between the Employee type and the emp variable because the Employee type now covers any string key.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev