I'm trying to convert a generic readonly string[] into an object with known keys. But I encounter the error:
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Record<T[number], number>'.
No index signature with a parameter of type 'string' was found on type 'Record<T[number], number>'.
Code:
class ArrayToObject<T extends readonly string[]> {
constructor (private keys : T) {}
GET_OBJECT () {
const all = {} as Record<T[number], number>
for (const index in this.keys) {
all[this.keys[index]] = index
}
return all;
}
}
What I basically want from the generic is something similar to this:
const a = ["a", "b"] as const
const o : Record<typeof a[number], number> = {
a : 1,
b : 2
}