Friday, April 4, 2014

TypeScript: using dynamic properties

Although I really like static typing and the other great features that TypeScript brings to JavaScript, sometimes I just want to profit from the dynamic nature of JavaScript.

I had a JavaScript/TypeScript object where I wanted to set a property value:

In TypeScript, this generates an error:

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

image

To fix it, I had to add a type annotation:

2 comments:

Anonymous said...

sweet. works!

Wanda said...

you could use

var product = {};
product["Name"] = "Duvel";