admin管理员组文章数量:1025539
export enum r {
color1 = "Red"
color2 = "green"
color3 = "blue"
}
ar = ["color1", "color2"];
ar.map(e => {
if (r[e as any] !== undefined) {
return r[e]
}
})
Above statement giving "Element implicitly has an any type because index expression is not of type number"
export enum r {
color1 = "Red"
color2 = "green"
color3 = "blue"
}
ar = ["color1", "color2"];
ar.map(e => {
if (r[e as any] !== undefined) {
return r[e]
}
})
Above statement giving "Element implicitly has an any type because index expression is not of type number"
Share Improve this question edited Jan 14, 2021 at 16:01 uminder 26.2k6 gold badges43 silver badges89 bronze badges asked Jan 14, 2021 at 15:47 user9485480user9485480 311 silver badge3 bronze badges2 Answers
Reset to default 7You need to type ar
with keyof typeof
to let TypeScript know your array contains keys from your enum:
export enum r {
color1 = "Red",
color2 = "green",
color3 = "blue",
}
const ar: (keyof typeof r)[] = ["color1", "color2"];
const newVal = ar.map((e) => {
if (r[e] !== undefined) {
return r[e];
}
});
Here is a good in-depth answer about it
You do not have mas after every item in your enum. However, the following is how you can typecast the argument to be type of key of the enum:
export enum r {
color1 = "Red",
color2 = "green",
color3 = "blue",
}
const ar = ["color1","color2"];
const new_ar = ar.filter(e => {
if (e in r && r[e as keyof typeof r])
{return true;}
else {return false;}
})
The problem with your approach is you are using map and you need to return in every iteration. What you need is filter.
export enum r {
color1 = "Red"
color2 = "green"
color3 = "blue"
}
ar = ["color1", "color2"];
ar.map(e => {
if (r[e as any] !== undefined) {
return r[e]
}
})
Above statement giving "Element implicitly has an any type because index expression is not of type number"
export enum r {
color1 = "Red"
color2 = "green"
color3 = "blue"
}
ar = ["color1", "color2"];
ar.map(e => {
if (r[e as any] !== undefined) {
return r[e]
}
})
Above statement giving "Element implicitly has an any type because index expression is not of type number"
Share Improve this question edited Jan 14, 2021 at 16:01 uminder 26.2k6 gold badges43 silver badges89 bronze badges asked Jan 14, 2021 at 15:47 user9485480user9485480 311 silver badge3 bronze badges2 Answers
Reset to default 7You need to type ar
with keyof typeof
to let TypeScript know your array contains keys from your enum:
export enum r {
color1 = "Red",
color2 = "green",
color3 = "blue",
}
const ar: (keyof typeof r)[] = ["color1", "color2"];
const newVal = ar.map((e) => {
if (r[e] !== undefined) {
return r[e];
}
});
Here is a good in-depth answer about it
You do not have mas after every item in your enum. However, the following is how you can typecast the argument to be type of key of the enum:
export enum r {
color1 = "Red",
color2 = "green",
color3 = "blue",
}
const ar = ["color1","color2"];
const new_ar = ar.filter(e => {
if (e in r && r[e as keyof typeof r])
{return true;}
else {return false;}
})
The problem with your approach is you are using map and you need to return in every iteration. What you need is filter.
本文标签:
版权声明:本文标题:javascript - How to access Enum in typescript ? giving error "Element implicitly has an any type because index expressi 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745628238a2159997.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论