Master TypeScript From Scratch

Learn TypeScript step by step with practical examples, interactive coding lessons, and real-world projects.

Object Destructuring in TypeScript

Learn how to extract object properties into variables, rename extracted properties, assign default values, destructure nested objects, and collect remaining properties using the rest syntax.

Simple Explanation: Object destructuring is a shorthand way to extract property values from an object into variables. It makes code shorter and easier to read.

What Is Object Destructuring?

Normally, we access an object property using dot notation or bracket notation. Destructuring allows us to extract multiple properties in a single statement.

TypeScript supports object destructuring and checks the extracted values according to the object's types.

  • Extract one or more properties into variables.
  • Rename extracted variables.
  • Assign default values when properties are undefined.
  • Extract values from nested objects.
  • Collect remaining properties using rest syntax.

Object Destructuring Workflow

Follow these steps to understand how properties are extracted from an object.

📦
Step 1
Create Object

Store information in named properties.

→
✂️
Step 2
Destructure

Select the required property names.

→
🔗
Step 3
Create Variables

Assign extracted property values to variables.

→
✅
Step 4
Use Values

Display or process the extracted data.

Visual Example
Original Object
{
    name: "Priya",
    age: 22,
    course: "TypeScript"
}
→
Destructuring
const {
    name,
    age
} = student;
→
Extracted Values

name = "Priya"

age = 22

1. Basic Object Destructuring

Object destructuring uses curly braces to specify which property values should be extracted from an object.

Syntax

const { property1, property2 } = objectName;

Example

let student = {
    name: "Rahul",
    age: 22,
    course: "TypeScript"
};

const { name, age } = student;

console.log(name);
console.log(age);

Expected Output

Rahul
22

Explanation

  • The object contains three properties.
  • The destructuring statement extracts only name and age.
  • The extracted values can be used as separate variables.

2. Property Access vs Destructuring

We can obtain the same property values using dot notation or object destructuring.

A. Using Dot Notation

const student = {
    name: "Priya",
    age: 21
};

const studentName = student.name;
const studentAge = student.age;

console.log(studentName);
console.log(studentAge);

B. Using Destructuring

const student = {
    name: "Priya",
    age: 21
};

const { name, age } = student;

console.log(name);
console.log(age);

Expected Output

Priya
21

Explanation

Both approaches extract the same values. Destructuring is convenient when we need multiple properties from the same object.

3. Renaming Variables During Destructuring

We can assign an extracted property to a variable with a different name by using a colon.

Syntax

const { propertyName: newVariableName } = objectName;

Example

const student = {
    name: "Priya",
    age: 22,
    course: "Java"
};

const {
    name: studentName,
    age: studentAge
} = student;

console.log(studentName);
console.log(studentAge);

Expected Output

Priya
22

Explanation

  • name is the original property name.
  • studentName is the new variable name.
  • The object's property names do not change. Only the local variable names are different.

4. Default Values in Object Destructuring

We can assign a default value to a destructured variable. The default is used when the property value is undefined.

Example

const student = {
    name: "Rahul",
    age: 22
};

const {
    name,
    course = "Not Selected"
} = student;

console.log("Name:", name);
console.log("Course:", course);

Expected Output

Name: Rahul
Course: Not Selected

Explanation

  • The object has no course property.
  • The destructuring statement assigns "Not Selected" to the course variable.
  • A default value is used only when the extracted value is undefined, not when it is null.

5. Renaming with Default Values

A destructured property can be renamed and assigned a default value in the same statement.

Example

const employee = {
    name: "Amit",
    salary: 30000
};

const {
    name: employeeName,
    department: employeeDepartment = "IT"
} = employee;

console.log("Employee:", employeeName);
console.log("Department:", employeeDepartment);

Expected Output

Employee: Amit
Department: IT

Explanation

The employee's name is extracted into employeeName. Because the department property is undefined, the default value "IT" is assigned to employeeDepartment.

6. Nested Object Destructuring

When an object contains another object as a property, we can destructure values from the nested object too.

Example

const student = {
    name: "Priya",
    address: {
        city: "Pune",
        pinCode: 411028
    }
};

const {
    name,
    address: { city, pinCode }
} = student;

console.log("Name:", name);
console.log("City:", city);
console.log("PIN Code:", pinCode);

Expected Output

Name: Priya
City: Pune
PIN Code: 411028

Explanation

  • The name property is extracted from the main object.
  • The city and pinCode properties are extracted from the nested address object.
  • The variable address itself is not declared by this destructuring pattern.

7. Rest Property in Object Destructuring

The rest property uses three dots to collect the remaining enumerable own properties that were not selected explicitly into a new object.

Example

const student = {
    name: "Rahul",
    age: 22,
    course: "TypeScript",
    city: "Pune"
};

const { name, ...otherDetails } = student;

console.log("Name:", name);
console.log("Other Details:", otherDetails);

Expected Output

Name: Rahul
Other Details: {
  age: 22,
  course: 'TypeScript',
  city: 'Pune'
}

Explanation

  • The name property is extracted separately.
  • The remaining properties are collected into otherDetails.
  • The original object is not modified by this operation.

8. Object Destructuring in Function Parameters

We can destructure an object directly in a function's parameter list. This is helpful when a function only needs specific properties from an object.

Example

type Student = {
    name: string;
    age: number;
    course: string;
};

function displayStudent({ name, course }: Student): void {
    console.log("Name:", name);
    console.log("Course:", course);
}

const student: Student = {
    name: "Priya",
    age: 22,
    course: "Dot Net Full Stack"
};

displayStudent(student);

Expected Output

Name: Priya
Course: Dot Net Full Stack

Explanation

  • The Student type defines the object's structure.
  • The function receives a student object.
  • The parameter destructuring extracts the name and course properties.

9. Destructuring with Type Annotations

TypeScript can infer the types of extracted variables from the source object's type. We can also provide a type annotation for the destructuring pattern.

Example

type Employee = {
    id: number;
    name: string;
    department: string;
};

const employee: Employee = {
    id: 101,
    name: "Amit",
    department: "Development"
};

const { id, name, department }: Employee = employee;

console.log("ID:", id);
console.log("Name:", name);
console.log("Department:", department);

Expected Output

ID: 101
Name: Amit
Department: Development

Explanation

The source object has the Employee type. TypeScript infers the types of the extracted variables from the corresponding properties. The explicit annotation is valid because the destructured values match the declared object structure.

10. Destructuring vs Property Access

Feature Property Access Destructuring
Syntax student.name const { name } = student
Multiple properties Usually accessed in separate expressions. Multiple properties can be extracted in one statement.
Rename variables Requires a separate variable declaration. Can rename a variable during extraction.
Default values Can use a separate fallback expression. Supports defaults directly in the pattern.
Nested properties Uses chained property access. Can extract nested properties in one pattern.

11. Practical Example: CIIT Student Details

Let's create a student object and use destructuring to extract the student's name, course, and marks.

Example

type Student = {
    id: number;
    name: string;
    course: string;
    marks: number;
    city: string;
};

const student: Student = {
    id: 101,
    name: "Priya",
    course: "Dot Net Full Stack",
    marks: 88,
    city: "Pune"
};

const {
    name: studentName,
    course,
    marks,
    ...otherDetails
} = student;

console.log("Student Name:", studentName);
console.log("Course:", course);
console.log("Marks:", marks);
console.log("Other Details:", otherDetails);

Expected Output

Student Name: Priya
Course: Dot Net Full Stack
Marks: 88
Other Details: { id: 101, city: 'Pune' }

Explanation

  • The student object stores all the student's details.
  • The name property is renamed to studentName.
  • The course and marks are extracted separately.
  • The rest property collects the remaining properties into the otherDetails object.

Common Mistakes

  • Using a variable name that does not match the object's property name when no rename is specified.
  • Confusing the colon used for renaming with a TypeScript type annotation.
  • Assuming default values replace both undefined and null.
  • Trying to destructure a nested property when the parent object may be undefined.
  • Forgetting that rest properties collect the remaining properties into a new object.
  • Using destructured variables without understanding whether they refer to values or nested object references.
Remember: Object destructuring does not rename or modify the original object's properties. It creates local bindings for the extracted values.

Practice Task

Write TypeScript programs to complete the following tasks.

  • Create an employee object and extract the employee's name and ID.
  • Rename a property during destructuring.
  • Assign a default value to an optional property.
  • Destructure the city and PIN code from a nested address object.
  • Use the rest property to collect the remaining properties of a product object.
  • Create a function that accepts an object and destructures its required properties.
Challenge: Create a student object, extract the name and course, rename the name variable, and collect all remaining properties into a separate object.

Summary

In this lesson, we learned how to extract object properties into variables using destructuring. We covered basic destructuring, variable renaming, default values, nested objects, rest properties, function parameters, and type annotations. Destructuring helps write cleaner and more readable TypeScript code.