TypeScript Object assign()

In this chapter, we will explore the Object.assign() method in TypeScript. This method copies the properties from one or more source objects to a target object. It returns the target object. This is useful for copying or merging objects.

Table of Contents

  1. Definition
  2. Syntax
  3. Examples
  4. Conclusion

1. Definition

The Object.assign() method copies the properties from one or more source objects to a target object. It returns the target object.

2. Syntax

Object.assign(target, ...sources);

Parameters

  • target: The object to which properties are copied.
  • sources: One or more objects from which properties are copied.

Return Value

The method returns the target object with properties copied from the source objects.

3. Examples

Let's look at some examples to understand how Object.assign() works in TypeScript.

Example 1: Basic Usage

This example shows how to copy properties from one object to another.

let target = {};
let source = { name: "Ravi", age: 25 };
Object.assign(target, source);
console.log(target); // Output: { name: 'Ravi', age: 25 }

Example 2: Merging Objects

This example shows how to merge properties from multiple source objects into a target object.

let target = { name: "Ravi" };
let source1 = { age: 25 };
let source2 = { city: "Mumbai" };
Object.assign(target, source1, source2);
console.log(target); // Output: { name: 'Ravi', age: 25, city: 'Mumbai' }

Example 3: Cloning an Object

This example shows how to create a shallow copy of an object.

let source = { name: "Ravi", age: 25 };
let clone = Object.assign({}, source);
console.log(clone); // Output: { name: 'Ravi', age: 25 }

Example 4: Overwriting Properties

This example shows how properties in the target object are overwritten by properties from the source objects.

let target = { name: "Ravi", age: 25 };
let source = { age: 30, city: "Mumbai" };
Object.assign(target, source);
console.log(target); // Output: { name: 'Ravi', age: 30, city: 'Mumbai' }

Example 5: Handling Undefined and Null Sources

This example shows how Object.assign() handles undefined and null source objects.

let target = { name: "Ravi" };
let source = null;
Object.assign(target, source);
console.log(target); // Output: { name: 'Ravi' }

source = undefined;
Object.assign(target, source);
console.log(target); // Output: { name: 'Ravi' }

Example 6: Copying Symbols

This example shows how to copy symbol properties from a source object to a target object.

let symbol = Symbol("id");
let target = {};
let source = { [symbol]: 123 };
Object.assign(target, source);
console.log(target); // Output: { [Symbol(id)]: 123 }

Example 7: Nested Objects

This example shows how Object.assign() handles nested objects. It performs a shallow copy, meaning nested objects are not deeply copied.

let target = { name: "Ravi", address: { city: "Mumbai" } };
let source = { address: { city: "Delhi" } };
Object.assign(target, source);
console.log(target); // Output: { name: 'Ravi', address: { city: 'Delhi' } }

4. Conclusion

In this chapter, we explored the Object.assign() method in TypeScript, which is used to copy properties from one or more source objects to a target object. We covered its definition, syntax, parameters, return value, and provided several examples to demonstrate its usage. Understanding how to use Object.assign() is essential for copying or merging objects in TypeScript applications.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare