marius marius - 1 month ago 3x
TypeScript Question

How to define Object of Objects type in typescript

I have an object to store cached data which should look like this:

private data = {
'some_thing': new DataModel(),
'another_name': new DataModel()

I'm trying to assign an empty object to it in the constructor: = {}; // produces build error

Basically, i need to define the type of "data" field to say that it's going to have keys with random names and values of type DataModel. I've tried to do this:

private data: Object<DataModel>

But this is invalid. How would i specify a correct type?


It should be:

private data: { [name: string]: DataModel };

And then this should work: = {};