I need to loop through an enum type to populate some options in a react component. Find below the enum and the function that retunrns the keys and values from it.
export enum ProyectType {
time,
hits,
value,
results
}
function projectTypeValues() {
const values = Object.keys(ProyectType).filter(k => typeof ProyectType[k as any] === "number"); // ["time", "hits", "value", "results"]
const keys = values.map(k => ProyectType[k as any]); // [0, 1, 2, 3]
}
I dont like the any
type in the ProyectType[k as any]
so I tried:
type EnumIndex = number | string;
ProyectType[k as EnumIndex]
But I obtain: Element implicitly has an 'any' type because index expression is not of type 'number'.ts(7015)
I thought the indexer could be of type number or string, as the the Object.Keys
is an array of the 8 elements: ["0","1","2","3","time","hits","value","results"]
but neither of both works.
How can the any
type be removed in this situation if the enum types are already known?