In JavaScript, objects contain key/value pairs that form the shape of the object. TypeScript supports object types as well, very similar to how you write plain JavaScript objects.
Consider the following plain JavaScript object:
const obj = {
x: 5,
y: 6
}
In the preceding code, obj is a plain object defined with two keys, x and y, both with number values.
To define an object type in TypeScript, you use a similar format — curly braces to represent an object, inside the keys, and their type information:
{ x:number, y:number }
Together, this is how you associate a type to an object:
const obj: { x:number, y:number } = {
x: 5,
y: 6
}
In the preceding code, the obj object is initialized the same as before, only now along with its type information, which is marked in bold.