Property does not exist in type 'object'

I have the following setup, and when I execute the loop using for...of and get the property property "country", does not exist in the type "object". Is this the right way to loop through each object in an array and compare the value of the property of the object?

 let countryProviders: object[]; export function GetAllProviders() { allProviders = [ { region: "r 1", country: "US", locale: "en-us", company: "co 1" }, { region: "r 2", country: "China", locale: "zh-cn", company: "co 2" }, { region: "r 4", country: "Korea", locale: "ko-kr", company: "co 4" }, { region: "r 5", country: "Japan", locale: "ja-jp", company: "co 5" } ] for (let providers of allProviders) { if (providers.country === "US") { // error here countryProviders.push(providers); } } } 
+5
source share
1 answer

You probably have allProviders as object[] . And the country property does not exist on object . If you do not want to print, you can declare both allProviders and countryProviders as Array<any> :

 let countryProviders: Array<any>; let allProviders: Array<any>; 

If you need static type checking. You can create an interface for the structure and use it:

 interface Provider { region: string, country: string, locale: string, company: string } let countryProviders: Array<Provider>; let allProviders: Array<Provider>; 
+11
source

Source: https://habr.com/ru/post/1266570/


All Articles