Is there a cleaner way to explicitly type an object and assign key-value pairs where the keys are variable names?
I found a solution using ES2015 template literals and JSON.parse()
that looks like this:
interface MyObj {
[propName: string]: number;
}
const myVar = 'key';
const objString = `{
"${myVar}": 1
}`;
const myObj: MyObj = JSON.parse(objString);
Although this implementation works, I'm concerned it may be challenging to manage in the future. Any suggestions for a more elegant or straightforward approach?