typescript add property to object code example
Example 1: conditionally add property to object ts
const a = {
...(someCondition && {b: 5})
}
Example 2: how to add property to object in javascript
var data = {
'PropertyA': 1,
'PropertyB': 2,
'PropertyC': 3
};
data["PropertyD"] = 4;
alert(data.PropertyD);
alert(data["PropertyD"]);
Example 3: typescript dynamic key value object
interface LooseObject {
[key: string]: any
}
var obj: LooseObject = {};
Example 4: ts create nex field
var obj:any = {}
obj.prop = 5;