admin管理员组文章数量:1290934
I'm trying to iterate over this enum. However I'm facing issues with extracting values from the enum. I can extract the value if i give it a hardcoded string like this.
export enum Values {
ACE = 'A',
TWO = '2',
THREE = '3',
FOUR = '4',
FIVE = '5',
SIX = '6',
SEVEN = '7',
EIGHT = '8',
NINE = '9',
TEN = '10',
JACK = 'J',
QUEEN = 'Q',
KING = 'K',
}
console.log(Values['TWO']);
But as soon as i give it the multiple string values from the enum like below.
export enum Values {
ACE = 'A',
TWO = '2',
THREE = '3',
FOUR = '4',
FIVE = '5',
SIX = '6',
SEVEN = '7',
EIGHT = '8',
NINE = '9',
TEN = '10',
JACK = 'J',
QUEEN = 'Q',
KING = 'K',
}
for (let value in Values) {
console.log(Values[value]);
}
It gives the following error:
error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'typeof Values'.
No index signature with a parameter of type 'string' was found on type 'typeof Values'.
25 console.log(Values[value]);
Completely new to TypeScript so might be missing something.
Cheers to anyone who could give me some guidance.
I'm trying to iterate over this enum. However I'm facing issues with extracting values from the enum. I can extract the value if i give it a hardcoded string like this.
export enum Values {
ACE = 'A',
TWO = '2',
THREE = '3',
FOUR = '4',
FIVE = '5',
SIX = '6',
SEVEN = '7',
EIGHT = '8',
NINE = '9',
TEN = '10',
JACK = 'J',
QUEEN = 'Q',
KING = 'K',
}
console.log(Values['TWO']);
But as soon as i give it the multiple string values from the enum like below.
export enum Values {
ACE = 'A',
TWO = '2',
THREE = '3',
FOUR = '4',
FIVE = '5',
SIX = '6',
SEVEN = '7',
EIGHT = '8',
NINE = '9',
TEN = '10',
JACK = 'J',
QUEEN = 'Q',
KING = 'K',
}
for (let value in Values) {
console.log(Values[value]);
}
It gives the following error:
error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'typeof Values'.
No index signature with a parameter of type 'string' was found on type 'typeof Values'.
25 console.log(Values[value]);
Completely new to TypeScript so might be missing something.
Cheers to anyone who could give me some guidance.
Share Improve this question edited Jun 19, 2021 at 11:48 ToxicTuring asked Jun 19, 2021 at 11:41 ToxicTuringToxicTuring 991 silver badge8 bronze badges 1- 1 codesandbox.io/s/fancy-star-hlg2f?file=/src/App.tsx – Sarun UK Commented Jun 19, 2021 at 11:48
1 Answer
Reset to default 9The type of the loop variable while iterating using for-in
loop over enum Values
is implicitly any
, so you get the pile error.
for (let value in Values) {
console.log(Values[value]);
}
The variable value
is of type any
, which cannot be used to index the enum Values
.
Open issue in TypeScript here.
If you want to iterate over the key-value pairs of the enum
you can do so with the help of Object.entries
:
enum Values {
ACE = 'A',
TWO = '2',
THREE = '3',
FOUR = '4',
FIVE = '5',
SIX = '6',
SEVEN = '7',
EIGHT = '8',
NINE = '9',
TEN = '10',
JACK = 'J',
QUEEN = 'Q',
KING = 'K',
}
//Both key and value
Object.entries(Values).forEach(([key, value]) => {
console.log(key, value)
});
//keys
Object.keys(Values).forEach(key => {
console.log(key);
});
//values
Object.values(Values).forEach(value => {
console.log(value);
});
本文标签: javascriptIterating over enums TypeScriptStack Overflow
版权声明:本文标题:javascript - Iterating over enums TypeScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741511108a2382611.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论