I am looking to iterate through an enum type in order to populate options within a react component. Below, you will find the specific enum and a function that retrieves its keys and values.
export enum TaskType {
daily,
weekly,
monthly,
yearly
}
function taskTypeValues() {
const values = Object.keys(TaskType).filter(k => typeof TaskType[k as any] === "number"); // ["daily", "weekly", "monthly", "yearly"]
const keys = values.map(k => TaskType[k as any]); // [0, 1, 2, 3]
}
Iām not comfortable using the any
type in TaskType[k as any]
, so I attempted:
type EnumIndex = number | string;
TaskType[k as EnumIndex]
However, I received this error:
Element implicitly has an 'any' type because index expression is not of type 'number'.ts(7015)
I believed that the indexer could be either a number or string, since Object.Keys
returns an array with 8 elements:
["0","1","2","3","daily","weekly","monthly","yearly"]
, but neither approach worked.
How can I eliminate the any
type in this scenario when the enum types are already defined?