Property does not exist on type Object in TypeScript

avatar

Borislav Hadzhiev

Last updated: Jul 25, 2022

banner

Photo from Unsplash

Property does not exist on type Object in TypeScript #

The "Property does not exist on type Object" error occurs when we try to access a property that is not contained in the object's type. To solve the error, type the object properties explicitly or use a type with variable key names.

Here is an example of how the error occurs.

index.ts
const obj: Object = { name: 'Tom', age: 30, }; // ⛔️ Error: Property 'country' does // not exist on type 'Object'.ts(2339) obj.country = 'Chile';

We typed the obj variable as Object and tried to access the country property on the object.

However, the country property does not exist on the Object type, so the type checker throws an error.

To solve the error, type the object explicitly to include any of the properties you intend to access.

index.ts
// ✅ When you know property names ahead of time type Person1 = { name: string; age: number; country?: string; }; const obj1: Person1 = { name: 'Tom', age: 30, }; obj1.country = 'Chile'; // ✅ When you don't know ALL property names ahead of time type Person2 = { [key: string]: any; // 👈️ variable keys name: string; }; const obj2: Person2 = { name: 'Tom', }; obj2.city = 'Santiago'; obj2.age = 30;

The first example shows how to type an object when you know its property names and the types of the values ahead of time.

You can mark properties that you won't provide when initializing the object as optional by using a question mark.

Now the country property exists in the object's type, so we are able to safely access it.

In some cases, you won't know the names of all of the object's keys or the shape of the values ahead of time.

index.ts
type Person2 = { [key: string]: any; name: string; }; const obj2: Person2 = { name: 'Tom', }; obj2.city = 'Santiago'; obj2.age = 30;

The {[key: string]: any} syntax is called an index signature and is used when you don't know the names of the object's keys or the shape of the values ahead of time.

The syntax means that when the object is indexed with a string key, it will return a value of any type.

If you know any of the names of the properties ahead of time, you can specify them to get better type safety.

We set the name property to string in the Person2 type, so the type checker would throw an error if the property is not provided, or is set to a value of a different type.

If you don't know the names of all of the object's keys, but know the shape of the values, you can use a more specific index signature for better type safety.

index.ts
type Person2 = { [key: string]: string | number; name: string; age: number; }; const obj2: Person2 = { name: 'Tom', age: 30, }; obj2.city = 'Santiago'; obj2.favNumber = 100;

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

The string | number syntax is called a union type in TypeScript.

We had to use a union type, because both the name and age properties are strings and they return different types.

In other words, you can't specify that when the object is indexed with a string key, it returns a value of type string, and then add another string key to the interface that has a value of type number.

index.ts
type Person2 = { [key: string]: string; name: string; // ⛔️ Error: Property 'age' of type 'number' // is not assignable to 'string' index type 'string'.ts(2411) age: number; };

The example shows that the type checker throws an error if we specify that when indexed with a string key, the object returns a value of type string, and we then try to add another string key that has a value of number.

Conclusion #

To solve the "Property does not exist on type Object" error, make sure to only access properties on the object that exist on the object's type. If you try to access a property that does not exist on the object's type, the error is thrown.

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.