Explorer
Typescript

TypeScript Object Types, Structural Typing & Utility Types

Learn how TypeScript understands object structures, checks compatibility, and creates reusable types without repeating code.

Imagine you're building a food-ordering application.

Every product has a name, price, availability status, and ingredients.

JAVASCRIPT
const tea = {
  name: "Masala Chai",
  price: 30,
  isHot: true
};

TypeScript automatically understands the structure of this object.

But what happens when you want to reuse the same structure for hundreds of products? Or create an update API where users can change only one property?

That's where object types, structural typing, and utility types become useful.

1. Object Types: Defining Data Structures

An object type describes which properties an object should contain and the types of those properties.

1.1 Automatic type inference

JAVASCRIPT
const tea = {
  name: "Masala Chai",
  price: 30,
  isHot: true
};

TypeScript infers:

JAVASCRIPT
{
  name: string;
  price: number;
  isHot: boolean;
}

If you assign an incompatible value:

JAVASCRIPT
tea.price = "Thirty"; // Type error

TypeScript detects the mismatch.

1.2 Explicit object types

You can define an object's structure manually:

JAVASCRIPT
let tea: {
  name: string;
  price: number;
  isHot: boolean;
};

tea = {
  name: "Masala Chai",
  price: 30,
  isHot: true
};

All required properties must be present.

Instead of repeating the same structure, create a reusable type alias:

JAVASCRIPT
type Tea = {
  name: string;
  price: number;
  isHot: boolean;
};

const order: Tea = {
  name: "Ginger Tea",
  price: 40,
  isHot: true
};

1.3 Nested objects and arrays

Object properties can contain other objects or arrays.

JAVASCRIPT
type Tea = {
  name: string;
  price: number;
  ingredients: string[];
  details: {
    isHot: boolean;
    size: string;
  };
};

This helps model structured data such as API responses, database records, and application state.

1.4 Optional and readonly properties

Not every property needs to be mandatory.

JAVASCRIPT
type Tea = {
  readonly id: number;
  name: string;
  price: number;
  description?: string;
};
  • description? makes the property optional.

  • readonly id prevents reassignment through that typed property.

These checks are enforced by TypeScript, not by automatically freezing the JavaScript object.

Remember: Object types describe the expected shape of data. They do not automatically validate objects received at runtime.

2. Structural Typing: If the Shape Matches, It Works

TypeScript uses a structural type system.

This means compatibility is primarily based on an object's properties and their types, rather than the name of the type.

It's often explained through duck typing:

"If it looks like a duck and walks like a duck, treat it like a duck."

2.1 Example: Two compatible objects

JAVASCRIPT
type Cup = {
  size: string;
};

let smallCup: Cup = {
  size: "200ml"
};

const bigCup = {
  size: "500ml",
  material: "steel"
};

smallCup = bigCup; // Valid

Why does this work?

The Cup type requires a size property of type string.

bigCup has that property, so it satisfies the required structure.

Its extra material property doesn't prevent the assignment.

2.2 What happens when a required property is missing?

JAVASCRIPT
const brokenCup = {
  material: "steel"
};

// smallCup = brokenCup; // Type error

The assignment fails because brokenCup does not contain the required size property.

2.3 Important exception: Excess property checks

Consider:

JAVASCRIPT
type Cup = {
  size: string;
};

const cup: Cup = {
  size: "200ml",
  material: "steel" // Type error
};

Why does TypeScript reject this when the previous example worked?

Fresh object literals receive additional excess property checks.

These checks help catch unexpected property names and common mistakes.

However, assigning an existing variable with a compatible structure generally allows extra properties.

Assignment

Result

Required properties match

Compatible

Required property missing

Type error

Existing object has extra properties

Usually allowed

Fresh object literal has unknown properties

Excess property error

Key takeaway: Structural typing checks whether a value satisfies the required structure, not whether the types share the same name.

3. Utility Types: Reusing Existing Types

Imagine you already have a Tea type:

JAVASCRIPT
type Tea = {
  name: string;
  price: number;
  isHot: boolean;
  secretIngredient?: string;
};

Now you need different versions of it:

  • One for updating a tea.

  • One where every property is mandatory.

  • One containing only basic information.

  • One without the secret ingredient.

You could manually write four new types, but that would duplicate code.

TypeScript provides utility types to transform existing types.

3.1 Partial: Make every property optional

JAVASCRIPT
type UpdateTea = Partial<Tea>;

Conceptually, this creates:

JAVASCRIPT
type UpdateTea = {
  name?: string;
  price?: number;
  isHot?: boolean;
  secretIngredient?: string;
};

Now you can provide only the properties you want to update.

JAVASCRIPT
const update: UpdateTea = {
  price: 35
};

When to use it: PATCH APIs, update forms, and partial configuration changes.

A common production pattern is to combine Partial with Pick to allow updates only to permitted fields.

3.2 Required: Make every property mandatory

JAVASCRIPT
type CompleteTea = Required<Tea>;

All properties become required, including secretIngredient.

JAVASCRIPT
const tea: CompleteTea = {
  name: "Masala Chai",
  price: 30,
  isHot: true,
  secretIngredient: "Cardamom"
};

Omitting any property produces a type error.

When to use it: When a particular operation requires all fields to be present.

3.3 Pick: Select specific properties

JAVASCRIPT
type TeaPreview = Pick<Tea, "name" | "price">;

This creates a type containing only:

JAVASCRIPT
type TeaPreview = {
  name: string;
  price: number;
};

Usage:

JAVASCRIPT
const preview: TeaPreview = {
  name: "Masala Chai",
  price: 30
};

When to use it: UI cards, summary data, and functions that require only selected fields.

3.4 Omit: Exclude specific properties

JAVASCRIPT
type PublicTea = Omit<Tea, "secretIngredient">;

Conceptually, this produces:

JAVASCRIPT
type PublicTea = {
  name: string;
  price: number;
  isHot: boolean;
};

When to use it: Defining object contracts that should not expose selected internal properties.

But there's an important limitation.

Omit only changes the TypeScript type. It does not remove properties from an existing JavaScript object.

JAVASCRIPT
const original = {
  name: "Masala Chai",
  price: 30,
  isHot: true,
  secretIngredient: "Cardamom"
};

const publicTea: PublicTea = original;

console.log(publicTea.secretIngredient);
// Type error, but the property still exists at runtime

If you're returning API data, explicitly construct a safe response object instead of relying on Omit to hide sensitive fields.

4. Utility Types in a Real API

Imagine you're building a product management API.

Your original product type is:

JAVASCRIPT
type Product = {
  id: string;
  name: string;
  price: number;
  stock: number;
};

Different operations need different shapes.

Creating a product

The database generates the id, so the client shouldn't supply it.

JAVASCRIPT
type CreateProduct = Omit<Product, "id">;

Updating a product

The client can update one or more editable properties.

JAVASCRIPT
type UpdateProduct = Partial<CreateProduct>;

Displaying a product card

The frontend needs only the name and price.

JAVASCRIPT
type ProductCard = Pick<Product, "name" | "price">;

Complete example

JAVASCRIPT
function updateProduct(
  id: string,
  changes: UpdateProduct
) {
  console.log(id, changes);
}

updateProduct("p1", {
  price: 499
});

This approach keeps the types connected to the original Product definition.

If a property type changes, the derived types reflect that change automatically.

Production note: Utility types provide compile-time contracts. Validate incoming API payloads at runtime, especially when enforcing allowed fields, numeric ranges, and required business rules.

5. Common Mistakes

Mistake

Better approach

Repeating object shapes everywhere

Create reusable types

Assuming extra properties are always accepted

Understand excess property checks

Using Partial<T> for every operation

Make only the intended fields optional

Assuming Omit removes runtime properties

Explicitly construct safe output objects

Assuming a matching type name is required

Remember structural typing

6. TypeScript Interview Questions

Q1. What is an object type in TypeScript?

An object type defines the expected structure of an object, including its properties, value types, and optional or readonly modifiers.

Q2. What is structural typing?

Structural typing determines compatibility based on required properties and their types, rather than the names of the types.

Q3. Why does TypeScript sometimes reject extra object properties?

Fresh object literals undergo excess property checks to catch unexpected property names. Existing variables with structurally compatible shapes can generally contain additional properties.

Q4. What is the difference between Partial and Required?

Partial<T> makes every property optional, while Required<T> makes every property mandatory.

Q5. What is the difference between Pick and Omit?

Pick<T, K> creates a type containing selected properties. Omit<T, K> creates a type excluding selected properties.

Q6. Does Omit remove sensitive properties at runtime?

No. Omit only affects static type checking. Runtime properties must be removed or excluded explicitly before data is exposed.

7. Quick Revision Notes

Core concepts

Concept

Remember

Object type

Defines an object's expected structure

Type inference

Automatically determines property types

Optional property

Uses ?

Readonly property

Prevents reassignment through the type

Structural typing

Compatibility based on shape

Excess property check

Additional check for fresh object literals

Partial<T>

Makes all properties optional

Required<T>

Makes all properties required

Pick<T, K>

Selects specified properties

Omit<T, K>

Excludes specified properties from a type

Essential syntax

Reusable object type

JAVASCRIPT
type User = {
  name: string;
  age: number;
};

Optional property

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

Structural typing

JAVASCRIPT
type Person = { name: string };

const employee = {
  name: "Rahul",
  role: "Developer"
};

const person: Person = employee; // Valid

Utility types

JAVASCRIPT
type UpdateUser = Partial<User>;
type CompleteUser = Required<User>;
type UserName = Pick<User, "name">;
type WithoutAge = Omit<User, "age">;

Five things to remember

  1. Object types define the structure of data.

  2. Structural typing compares required properties, not type names.

  3. Fresh object literals receive excess property checks.

  4. Utility types reduce duplication by deriving types from existing definitions.

  5. Utility types do not change runtime JavaScript objects.

Final takeaway

TypeScript allows developers to model complex data without repeatedly defining the same structures.

Structural typing makes object compatibility flexible, while utility types help derive smaller, stricter, or partially optional versions of existing types.

Define your data shape once, reuse it where possible, and remember that static types do not replace runtime validation.


Further Reading


Finished this lesson?

Mark this chapter complete to update your learning streak and unlock the next lesson.