Is there a proper way to refactor the creation of dynamic variables in TypeScript? I am encountering an error when trying to assign a value to obj.identifier which is not known. What would be the correct approach for refactoring this scenario?
interface test {
[key: string]: any
}
class Test implements test {
#obj: any
constructor(obj: any) {
this.#obj = obj
}
test(value: string) {
// Type 'string' is not assignable to type '(value: string) => void'
this[this.#obj.identifier as keyof Test] = value
}
}
I'm looking to use it in the following way:
const t = new Test({identifier: "a"})
t.test("aa")
t.a
=> "aa"