DEV Community

Cover image for ERROR:'XYZ' property doesn't exist on the type 'ABC'
Ruta
Ruta

Posted on

ERROR:'XYZ' property doesn't exist on the type 'ABC'

I was trying to map nested object, and I got this error
'XYZ' property doesn't exist on the type 'ABC'

Image description

I was accessing the properties using dot notation in get-lodash function. It didn't work so I got to know another approach to access properties. It is square brackets notation.

Let me elaborate using example
This is the Object

let company = {
    name: "Amazon",
    contact: {
        phone: "88076281093",
        email: "amazon123@xyz.com",
        address: {
            city: "Pune",
            country: "India"
        }
    }
};

Enter fullscreen mode Exit fullscreen mode

Dot notation to access it :

console.log(company.contact.email);              
console.log(company.contact.address.city);`
Enter fullscreen mode Exit fullscreen mode

Square Bracket notation to access it :

console.log(person["contact"]["email"]);             
console.log(person["contact"]["address"]["country"]);

Enter fullscreen mode Exit fullscreen mode

OR

console.log(person["contact","email"]);             
console.log(person["contact","address","country"]);

Enter fullscreen mode Exit fullscreen mode

Also in #typescript when you define type for nested object you can do it in this way

let company = {
name: string,
phone: number,
email: string,
city: string,
country: string
}
Enter fullscreen mode Exit fullscreen mode

Hope it helps if you are struggling..

Reference link:https://www.geeksforgeeks.org/how-to-access-and-process-nested-objects-arrays-or-json/

Top comments (1)

Collapse
 
shreyakothawale profile image
Shreya Kothawale

Informative