is it possible to use an object spread inside a template string:
const obj = {
key1: 'value1',
key2: 'value2'
};
// contrived example to show that `obj` can also be dynamically constructed
for (let i = 0; i < 3; i++) {
obj[`someKey${i}`] = i
}
const templateString = `{
"templateKey1": "anotherValue1",
"templateKey2": "anotherValue2",
${...obj}
}`
expected result:
console.log(templateString)
// should output an object in string format:
'{
"templateKey1": "anotherValue1",
"templateKey2": "anotherValue2",
"key1": "value1",
"key2": "value2",
"someKey0": 0,
"someKey1": 1,
"someKey2": 2
}'
I only get an Unexpected token
error message when it tries compiling.
My project is using babel and the object spread plugin works as intended so the configuration is not incorrect.