57

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:

this.data = {}; // 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?

1 Answer 1

168

It should be:

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

And then this should work:

this.data = {};
Sign up to request clarification or add additional context in comments.

1 Comment

Wish I give give this a million up votes! Thanks mate!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.