The Javascript code below prints { x: 1 }:
var foo = 'x';
var a = {};
if (foo == 'x')
a.x = 1;
else
a.y = 2;
console.log(a);
Similarly, I need to dynamically build in Typescript an object, in other words when I create the object i don't know what are going to be its members.
I tried this in Typescript, but it doesn't work:
let foo = 'x';
let a = {};
if (foo == 'x')
a.x = 1; // <-- transpilation fails here
else
a.y = 2; // <-- transpilation fails here
console.log(a);
The error is Property 'x' does not exist on type '{}'. Is it possible to create objects dynamically in Typescript?