TYPESCRIPT

TypeScript Object Type: Syntax, Usage, and Examples

The TypeScript object type allows you to define the structure and shape of an object with static typing. It helps you write code that checks for key names and value types before runtime. With object type TypeScript support, you gain confidence that your objects behave exactly as expected.

How to Use Object Types in TypeScript

You can define an object type in TypeScript in a few different ways, depending on the complexity of the structure. Here are the basics:

Defining an Inline Object Type

let user: { name: string; age: number } = {
  name: "Alice",
  age: 30,
};

This defines the type directly when declaring the variable.

Using an Interface

interface User {
  name: string;
  age: number;
}

let user: User = { name: "Bob", age: 25 };

Interfaces are reusable and make your code easier to read and maintain.

Using a Type Alias

type Product = {
  id: number;
  title: string;
};

let item: Product = { id: 1, title: "Book" };

You can use both interface and type for defining object type TypeScript structures, depending on your preferences and needs.

When to Use Object Types in TypeScript

You should use the TypeScript object type when:

  • You're working with data structures that require specific property names and types.
  • You need to define API response formats.
  • You're passing structured props to components in React.
  • You want to create reusable, strongly typed objects for state, configuration, or user inputs.

TypeScript object types are everywhere in modern apps. Once you start using them, you’ll quickly see how much cleaner and safer your code becomes.

Examples of Using Object Type in TypeScript

TypeScript Empty Object Type

If you want to allow any object but no specific properties:

let config: {} = {};

This allows assignment of any non-primitive object, but you can’t access any properties without further checks. A better approach is often:

let settings: Record<string, unknown> = {};

This gives you an empty object shape but allows key-value pairs to be added dynamically.

TypeScript Mapped Object Type

You can define dynamic key names using mapped types:

type Permissions = "read" | "write" | "delete";

type UserPermissions = {
  [K in Permissions]: boolean;
};

const admin: UserPermissions = {
  read: true,
  write: true,
  delete: true,
};

This creates an object where each key is one of the union members. You get full autocomplete and type checking.

TypeScript Generic Object Type

You can make object types more flexible by using generics:

type Wrapper<T> = {
  data: T;
  timestamp: Date;
};

const userWrapper: Wrapper<{ name: string }> = {
  data: { name: "Dana" },
  timestamp: new Date(),
};

This is useful for API responses, caching, or tracking metadata alongside your data.

TypeScript Get Type of Object

You can infer the object type from a value:

const car = {
  make: "Toyota",
  year: 2021,
};

type Car = typeof car;

The typeof operator pulls out the type from an existing value. This is great when you want to avoid repeating the shape.

TypeScript Check if Object Is of Type

At runtime, TypeScript types don’t exist. But you can use type guards to check if an object matches a known shape:

type Person = { name: string; age: number };

function isPerson(obj: any): obj is Person {
  return (
    typeof obj === "object" &&
    typeof obj.name === "string" &&
    typeof obj.age === "number"
  );
}

This lets you safely use the object as a Person after the check.

Learn More About TypeScript Object Types

Using Optional and Readonly Properties

You can mark properties as optional with ?:

type User = {
  name: string;
  email?: string;
};

This allows email to be undefined. For immutable fields, use readonly:

type Config = {
  readonly id: number;
  editable: boolean;
};

Trying to change id will cause a type error.

Nested Object Types

Objects inside objects? No problem.

type Author = {
  name: string;
  address: {
    city: string;
    country: string;
  };
};

const author: Author = {
  name: "Lee",
  address: {
    city: "Austin",
    country: "USA",
  },
};

You can nest object types as deeply as you need, and TypeScript will check every level.

Combining Object Types with Intersection

Need to combine two different object types? Use the & operator:

type Timestamps = { createdAt: Date; updatedAt: Date };
type Post = { title: string; content: string };

type BlogPost = Post & Timestamps;

Now BlogPost has all the fields from both Post and Timestamps.

Utility Types That Work With Object Type

TypeScript includes many built-in utility types that help you manipulate object types:

  • Partial<T> – makes all properties optional
  • Required<T> – makes all properties required
  • Pick<T, K> – creates a type with selected keys
  • Omit<T, K> – creates a type with selected keys removed
  • Record<K, T> – creates an object type with keys K and values T

Example using Pick:

type FullUser = { id: number; name: string; email: string };
type PublicUser = Pick<FullUser, "id" | "name">;

Now PublicUser only includes id and name.

Working with Records

You can create flexible objects with string keys using Record:

type ScoreBoard = Record<string, number>;

const scores: ScoreBoard = {
  Alice: 90,
  Bob: 80,
};

This lets you use dynamic key names while keeping value types strict.

Index Signatures

Use index signatures for objects where the key isn’t known in advance:

type Dictionary = {
  [key: string]: string;
};

const colors: Dictionary = {
  red: "#ff0000",
  blue: "#0000ff",
};

This is useful for settings, caches, or key-value stores.

TypeScript Objects and Functions

Functions can also live inside objects:

type Button = {
  label: string;
  onClick: () => void;
};

const myButton: Button = {
  label: "Submit",
  onClick: () => {
    console.log("Clicked!");
  },
};

Object types work beautifully with event handlers and UI components.

Best Practices with Object Type in TypeScript

  • Define reusable object types with interface or type.
  • Use optional fields for flexibility but don’t overuse them.
  • Prefer readonly for values that should never change.
  • Use generics when the object shape depends on input types.
  • Use utility types like Partial, Pick, or Record to avoid duplication.
  • Use type guards when checking types at runtime.

The TypeScript object type gives you the power to describe any structure with clarity and safety. Whether you're defining complex API responses, building form models, or enforcing rules across app settings, object type TypeScript support keeps your code honest and maintainable.

Learn TypeScript for Free
Start learning now
button icon
To advance beyond this tutorial and learn TypeScript by doing, try the interactive experience of Mimo. Whether you're starting from scratch or brushing up your coding skills, Mimo helps you take your coding journey above and beyond.

Sign up or download Mimo from the App Store or Google Play to enhance your programming skills and prepare for a career in tech.

You can code, too.

© 2025 Mimo GmbH