How To Solve Property does not exist on type ‘{}’ in TypeScript

The Property does not exist on type ‘{}’ in TypeScript occurs when we access a property that does not exist in the object. This article will show you ways to fix the error, such as Explicitly adding properties and using variable keys. Let’s start learning.

What causes Property does not exist on type ‘{}’ in TypeScript?

Attempting to modify, set or access a property that does not exist in an object will result in a Property does not exist on type ‘{}’ error in the TypeScript. For example, suppose you have an object with only two properties and try to access a third property that does not exist on this object type. You can work around the above errors by explicitly adding object properties or using variables with the same essential type or name. TypeScript type checking can be enabled or disabled at any time. It’s not used at all, but we wouldn’t recommend using it often in large projects.

Error Example:

const student = {};

// We access the property name which is not present in the student object.
student.name = "John";

Output:

Property 'name' does not exist on type '{}'.

Based on the above, assume that ‘student’ is JSON data obtained from a specific API (application programming interface) as a result of an HTTP request while the application is running. Because you haven’t defined the object itself, so all it can do is read only available features and an empty object is defined. Therefore, you cannot set a value or access a property that does not exist in the object type.

Solution for Property does not exist on type ‘{}’ in TypeScript.

Explicitly properties

To fix the property that doesn’t exist in typescript ‘{}’, remember to only access existing object keys when using node if your object does not have a unique identifier. To avoid this error, you must explicitly add the key in case you know all the required properties of the object. You can explicitly define the object and include all the properties that you want to access.

Example:

// Explicitly add the key in case you know all the required properties of the object.
type Car = {
	manufacturer?: string;
	model?: string;
};

const car: Car = {};
car.manufacturer = "Honda";
car.model = "Civic";
console.log(car);

Output:

{ manufacturer: 'Honda', model: 'Civic' }

Define a variable key

Example:

type Student = {
  	// Define a variable key
  	[key: string]: any;
	name: string;
};

const student: Student = {
	name: "John",
};

student.subject = "Software technology";
console.log(student);

Output:

{ name: 'John', subject: 'Software technology' }

Summary

Through this article, we found out why TypeScript is giving an error Property does not exist on type ‘{}’. To fix this error, remember to only access existing object keys when working with nodes. If the specified key does not exist in the object, it must be explicitly added the key to avoid this error. Thanks for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *