patternjavascriptCritical
Typescript : Property does not exist on type 'object'
Viewed 0 times
typescriptexistpropertyobjectdoesnottype
Problem
I have the follow setup and when I loop through using
Property "country" doesn't exist on type "object".
Is this a correct way to loop through each object in array and compare the object property value?
for...of and get an error of :Property "country" doesn't exist on type "object".
Is this a correct way to loop through each object in array and compare the object property value?
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);
}
}
}Solution
You probably have
If you do want static type checking. You can create an interface for the structure and use it:
allProviders typed as object[] as well. And property country does not exist on object. If you don't care about typing, you can declare both allProviders and countryProviders as Array:let countryProviders: Array;
let allProviders: Array;If you do want 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;
let allProviders: Array;Code Snippets
let countryProviders: Array<any>;
let allProviders: Array<any>;interface Provider {
region: string,
country: string,
locale: string,
company: string
}
let countryProviders: Array<Provider>;
let allProviders: Array<Provider>;Context
Stack Overflow Q#43338763, score: 165
Revisions (0)
No revisions yet.