CPCODELAB
TypeScript

Object Types & Type Aliases

10 min read

Object Types & Type Aliases

In TypeScript you can describe the shape of an object inline or give it a name using a type alias. An inline object type lists the property names and their types inside curly braces.

ts
// Inline object type
function greet(user: { name: string; age: number }) {
  return `Hello, ${user.name}!`;
}

// Type alias
type User = {
  id: number;
  name: string;
  email: string;
  role: "admin" | "student";
};

const alice: User = { id: 1, name: "Alice", email: "a@example.com", role: "admin" };

Type aliases can name any type, not just objects. They can alias primitives, unions, tuples, or even function signatures.

ts
type ID = string | number;
type Point = [number, number];
type Callback = (event: string) => void;

type WithTimestamps<T> = T & {
  createdAt: Date;
  updatedAt: Date;
};

type UserWithTimestamps = WithTimestamps<User>;

Use a type alias when you need to name a union, a tuple, or a computed type. For plain object shapes that might need declaration merging, prefer an interface (covered in the next lesson).

Ready to test yourself?
Practice TypeScript— quiz & coding exercises