How do I dynamically assign properties to an object in TypeScript?
@lebyy▲ 0 net votes
Problem: If I wanted to programmatically 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 do I assign any… Fix (from the top answer): Index signatures It is possible to denote obj as any , but that defeats the whole purpose of using typescript. obj = {} implies obj is an Object . Marking it as any makes no sense. To accomplish the desired consistency an interface could be defined as follows, using an index signature interface LooseObject { [key:…