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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var product = {}; | |
product.Name = "Duvel"; |
In TypeScript, this generates an error:
The property 'prop' does not exist on value of type '{}'
To fix it, I had to add a type annotation:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var product:any = {}; | |
product.Name = "Duvel"; |