admin管理员组文章数量:1134246
I have the follow setup and when I loop through using 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);
}
}
}
I have the follow setup and when I loop through using 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);
}
}
}
Share
Improve this question
edited Nov 11, 2020 at 8:00
Boussadjra Brahim
1
asked Apr 11, 2017 at 6:55
JasonJason
1,8583 gold badges14 silver badges17 bronze badges
5 Answers
Reset to default 165You probably have 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<any>
:
let countryProviders: Array<any>;
let allProviders: Array<any>;
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<Provider>;
let allProviders: Array<Provider>;
If your object contains any key/value pairs, you could declare an interface called keyable
like :
interface keyable {
[key: string]: any
}
then use it as follows :
let countryProviders: keyable[];
or
let countryProviders: Array<keyable>;
For your specific case try to define the key type and use Record
utility to define the object :
type ProviderKey="region" | "country" | "locale" | "company"
let countryProviders: Array<Record<ProviderKey,string>>;
Yes the above methods are highly recommended, but if you have no choice and want to continue using "allProviders" as an array of object, then go for this method. This worked for me without creating an interface.
if(providers["country"] === "US") // replacement
Yes you should try to avoid object
, any
etc. if you can, after all the thing is called TypeScript for a reason.
If, however, you need a working dirty trick:
let o: object = { id: 1 }
console.log(o.id); // throws error
console.log((o as any).id); // fine
As always: anything goes with any.
If you want to strongly type, you could do
type ProviderKeys = 'region' | 'country' | 'locale' | 'company'
type Provider = Record<ProviderKeys, string>;
const countryProviders: Provider[] = [];
let allProviders: Provider[] = []
本文标签: javascriptTypescriptProperty does not exist on type 39object39Stack Overflow
版权声明:本文标题:javascript - Typescript : Property does not exist on type 'object' - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736818158a1954176.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论