How to define Object of Objects type in typescript

marius

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?

Nitzan Tomer

It should be:

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

And then this should work:

this.data = {};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Typescript: define type of an object

From Java

How to define a Typescript object type based on existing object values?

From Java

How to make Typescript infer the keys of an object but define type of its value?

From Dev

How to define an object with different type of value correctly in Typescript

From Java

How to define an opaque type in TypeScript?

From Dev

How to define a object type in a function?

From Dev

How define a generic type from an object type?

From Dev

How to define type of an object whose properties are going to be fetched from an array in TypeScript

From Dev

In typescript, how to define type of async function

From Dev

Typescript: How to define overloaded function type

From Java

Typescript: define object of type, but infer provided optional props as required

From Dev

How to type an object key in Typescript?

From Java

How can I define an interface for an array of objects with Typescript?

From Java

Typescript: How do I define interfaces for nested objects?

From Dev

How can I define an array of objects using Typescript?

From Dev

How to define new object of variable type of array object?

From Dev

How to define prop validation rule for an object of objects in React?

From Dev

How can I define a return type of void for a function in a Typescript interface?

From Java

How to define string literal union type from constants in Typescript

From Dev

How should I define the type of a promise call back in Typescript?

From Dev

How can I define the return type of a lodash reduce function with Typescript?

From Dev

Typescript - How can I conditionally define a type based on another property

From Dev

How can I define the return type of a lodash reduce function with Typescript?

From Dev

How can I define a Typescript object return value for a function?

From Dev

How can I define the types of an object variable in Typescript?

From Dev

How to create many objects by passing the object type?

From Dev

How to create many objects by passing the object type?

From Dev

Is in Typescript possible to define 'object of arrays of type string? (right position of type checking?)

From Dev

How to tell TypeScript the type of my object?

Related Related

  1. 1

    Typescript: define type of an object

  2. 2

    How to define a Typescript object type based on existing object values?

  3. 3

    How to make Typescript infer the keys of an object but define type of its value?

  4. 4

    How to define an object with different type of value correctly in Typescript

  5. 5

    How to define an opaque type in TypeScript?

  6. 6

    How to define a object type in a function?

  7. 7

    How define a generic type from an object type?

  8. 8

    How to define type of an object whose properties are going to be fetched from an array in TypeScript

  9. 9

    In typescript, how to define type of async function

  10. 10

    Typescript: How to define overloaded function type

  11. 11

    Typescript: define object of type, but infer provided optional props as required

  12. 12

    How to type an object key in Typescript?

  13. 13

    How can I define an interface for an array of objects with Typescript?

  14. 14

    Typescript: How do I define interfaces for nested objects?

  15. 15

    How can I define an array of objects using Typescript?

  16. 16

    How to define new object of variable type of array object?

  17. 17

    How to define prop validation rule for an object of objects in React?

  18. 18

    How can I define a return type of void for a function in a Typescript interface?

  19. 19

    How to define string literal union type from constants in Typescript

  20. 20

    How should I define the type of a promise call back in Typescript?

  21. 21

    How can I define the return type of a lodash reduce function with Typescript?

  22. 22

    Typescript - How can I conditionally define a type based on another property

  23. 23

    How can I define the return type of a lodash reduce function with Typescript?

  24. 24

    How can I define a Typescript object return value for a function?

  25. 25

    How can I define the types of an object variable in Typescript?

  26. 26

    How to create many objects by passing the object type?

  27. 27

    How to create many objects by passing the object type?

  28. 28

    Is in Typescript possible to define 'object of arrays of type string? (right position of type checking?)

  29. 29

    How to tell TypeScript the type of my object?

HotTag

Archive