Peter Olson Peter Olson - 3 months ago 13
TypeScript Question

How do I dynamically assign properties to an object in TypeScript?

If I wanted to programatically assign a property to an object in Javascript, I would do it like this:

var obj = {};
obj.prop = "value";


But in TypeScript, this generates an error:


The property 'prop' does not exist on value of type '{}'


How am I supposed to assign any new property to an object in TypeScript?

Answer

Or all in one go:

  var obj:any = {}
  obj.prop = 5;