In TypeScript, an object is a structured data type that represents a collection of properties, where each property consists of a key-value pair.

Each property in an object can have a specific type, and the object itself can be annotated with a type definition. This type definition is usually created using either a type alias or an interface.

TypeScript allows you to define the shape of an object, meaning you can specify what keys an object should have and what type of values those keys should hold.

TypeScript uses structural typing, which means the structure (shape) of the object matters more than its name or declaration.

If two objects have the same shape (same keys and value types), they are considered type-compatible even if they were declared separately.

Understanding Object Types

When you create an object in TypeScript using the familiar {...} syntax, you're not just creating a simple bundle of data—you're also giving it a unique blueprint, or "type shape." TypeScript takes a look at the properties and their values inside your object and automatically figures out what this blueprint looks like.

image.png

Declaring Object Types

While TypeScript is great at automatically figuring out the type of an object, you'll eventually want to be able to explicitly declare that type yourself. This is especially useful when you need to define a reusable blueprint for multiple objects, or when you want to make sure an object you create later on fits a specific shape.

You can describe an object's shape using a syntax that looks very similar to an object literal, but instead of providing values, you provide the types for each property. This creates a clear, formal contract for what an object must contain.

image.png

Aliased Object Types

Constantly writing out object types with the verbose { ... } syntax would quickly become tiresome and make your code difficult to read. A much more common and elegant solution is to use type aliases to give your object shapes a meaningful name.

image.png

This approach not only makes your code cleaner and more reusable, but it also improves the clarity of TypeScript's error messages.

image.png