. Advertisement .
..3..
. Advertisement .
..4..
You may find TypeScript and its optional static typing appealing. However, it comes with a whole host of differences with JavaScript, such as Property does not exist on type Object in TypeScript. Read on if you want to understand more about this error message and how to get rid of it.
Property Does Not Exist On Type Object In TypeScript
Why Does This Error Occur?
While it is based on JavaScript, TypeScript has many different behaviors you may feel unfamiliar with when trying to develop a project with it. For instance, some compiled programs don’t give any error in JavaScript, but their original TypeScript does.
Consider this program written in TypeScript:
type Person = {
name: string;
age: number;
job: string;
birthyear: number;
};
function address(person: Person) {
console.log(person.address);
}
const john: Person = {
name: "Johnson",
age: 45,
job: "accountant",
birthyear: 1985
}
address(john);
Output:
undefined
Property 'address' does not exist on type 'Person'.
It returns an undefined value and error message at the same time. The reason is the Person object type doesn’t have any property named address, which is what the address() function tries to access.
If you compile it to JavaScript, however, the program only gives an undefined output.
function address(person) {
console.log(person.address);
}
const john = {
name: "Johnson",
age: 45,
job: "accountant",
birthyear: 1985
};
address(john);
Output:
undefined
There is no address property in the john object. But the address() function in JavaScript doesn’t consider this an error. What you receive is an undefined value, which doesn’t cause the “Object is possibly ‘undefined’” error.
How To Fix The Issue
The most obvious solution for this error is to define the property in question when declaring the object type.
For example, the previous TypeScript code can be fixed by adding the address property into the Person object type.
type Person = {
name: string;
age: number;
job: string;
birthyear: number;
address: string;
};
function address(person: Person) {
console.log(person.address);
}
const john: Person = {
name: "Johnson",
age: 45,
job: "accountant",
birthyear: 1985,
address: "Seattle"
}
address(john);
Output:
Seattle
You can see that now the address() function does its job without a hitch. But what if you don’t have any value for the mentioned property?
TypeScript allows you to use the question mark (?) modifier to mark a property as optional instead.
type Person = {
name: string;
age: number;
job: string;
birthyear: number;
address?: string;
};
function address(person: Person) {
console.log(person.address);
}
const john: Person = {
name: "Johnson",
age: 45,
job: "accountant",
birthyear: 1985,
}
address(john);
Output:
undefined
The address property is now optional in the Person object type. You can declare an instance without that key, and TypeScript will give you no error. Remember that it still returns the undefined value.
There is another solution based on index signatures, which helps define object types in TypeScript when you only know the types of property and value.
Example:
type Person = {
[key: string]: any;
name: string;
age: number;
job: string;
birthyear: number;
};
function address(person: Person) {
console.log(person.address);
}
const john: Person = {
name: "Johnson",
age: 45,
job: "accountant",
birthyear: 1985,
}
address(john);
Output:
undefined
The declaration above tells TypeScript that Person has the string type as properties and values. You can, of course, give the john instance an address property too.
type Person = {
[key: string]: any;
name: string;
age: number;
job: string;
birthyear: number;
};
function address(person: Person) {
console.log(person.address);
}
const john: Person = {
name: "Johnson",
age: 45,
job: "accountant",
birthyear: 1985,
address: "Seattle"
}
address(john);
Output:
Seattle
Conclusion
Property does not exist on type Object in TypeScript is an error arising from an object type declaration that lacks such property. You have several options for solving this: declare the property, make it optional, or define an index signature.
Leave a comment