Property is incompatible with index signature in TypeScript

avatar
Borislav Hadzhiev

Last updated: Feb 29, 2024
3 min

banner

# Property is incompatible with index signature in TypeScript

The error "Property is incompatible with index signature" occurs when a property is not compatible with the specified type of the index signature.

To solve the error, change the type of the property or use a union to update the type in the index signature.

property incompatible with index signature

Here is an example of how the error occurs.

index.ts
type Employee = { [key: string]: string; }; const dev = { salary: 100, // ๐Ÿ‘ˆ๏ธ this is a number name: 'Bobby Hadz', }; // โ›”๏ธ Error: Property 'salary' is incompatible with index signature. const emp1: Employee = dev;

property is incompatible with index signature in typescript

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

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

However, notice that the salary property in the object has a value of type number.

This causes the error because the type of the salary property is incompatible with the index signature.

# Use a union type to solve the error

One way to solve the error is to use a union type to widen the type of the values in the index signature.

index.ts
type Employee = { [key: string]: string | number; // ๐Ÿ‘ˆ๏ธ now takes string or number }; const dev = { salary: 100, name: 'Bobby Hadz', }; const emp1: Employee = dev;

using union type to solve the error

The code for this article is available on GitHub

We don't get the error anymore because now the index signature in the Employee type means that when the object is indexed with a string key, it will return a value of type string or number.

# Turning off type checking with the any type

If you don't know the names of the properties or the type of the values your object will contain and want to turn off type checking, set the type in the index signature to any.

index.ts
type Employee = { [key: string]: any; }; const dev = { salary: 100, name: 'Bobby Hadz', tasks: ['develop', 'test'], }; const emp1: Employee = dev;

turning off type checking with any type

The code for this article is available on GitHub

This approach enables you to add properties of any type to the object, but it disables type checking.

# Another example of how the error occurs

Here is another example of how the error occurs.

index.ts
type Employee = { [key: string]: string; }; function example(emp: Employee) { return emp; } const emp2 = { id: 1, name: 'Bobby Hadz' }; // โ›”๏ธ Property 'id' is incompatible with index signature. example(emp2);

The index signature in the Employee type only allows values of type string, however, the id property in the object has a value of number.

To solve the error, we either have to widen the type in the index signature or set the id property to a string value in the object.

index.ts
type Employee = { [key: string]: string | number; // ๐Ÿ‘ˆ๏ธ widen type }; function example(emp: Employee) { return emp; } const emp2 = { id: 1, name: 'Alice' }; // โœ… works now example(emp2);

widen type in index signature

The code for this article is available on GitHub

# 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