An object in TypeScripts has a special form of declaring because it can be declared as an interface, as a direct object, or as a type of its own.
Declaring an object as an interface, you have to declare the interface before using it, all the attributes must be passed, and the types need to be set:
interface IPerson {
name: string;
age: number;
}
const person: IPerson = {
name: 'Heitor',
age: 31,
};
Using objects as direct inputs is sometimes common when passing to a function:
function greetingUser(user: {name: string, lastName: string}) {
console.log(`Hello, ${user.name} ${user.lastName}`);
}
And finally, they are used for declaring a type of object and reusing it:
type Person = {
name: string,
age: number,
};
const person: Person = {
name: 'Heitor',
age: 31,
};
console.log(`My name is ${person.name}, I am ${person.age} years old`);