Sometimes, we want to define an object of objects type in TypeScript.
In this article, we’ll look at how to define an object of objects type in TypeScript.
How to define an object of objects type in TypeScript?
To define an object of objects type in TypeScript, we can use index signatures with the type set to the type for the value object.
For instance, we write
const data: { [name: string]: DataModel } = {
//...
};
to create a data
variable of type { [name: string]: DataModel }
where DataModel
is an object type.
[name: string]
is the index signature that lets us add any property with string keys.
As long as the structure of data
matches the structure specified in the type, the type check will pass.
Conclusion
To define an object of objects type in TypeScript, we can use index signatures with the type set to the type for the value object.