How to Merge Objects in TypeScript

Avatar

By squashlabs, Last Updated: May 8, 2024

How to Merge Objects in TypeScript

In TypeScript, merging objects refers to combining the properties of two or more objects into a single object. This can be useful in various scenarios, such as combining configurations, merging data from multiple sources, or creating a new object with updated properties.

There are several techniques available in TypeScript to merge objects, each with its specific use cases and advantages. In this tutorial, we will explore these techniques and learn how to merge objects effectively in TypeScript.

Techniques for Merging Objects in TypeScript

TypeScript provides several techniques for merging objects, including merging two objects, merging properties of objects, object spreading, using Object.assign, merging objects with same keys, merging objects with different keys, and merging nested objects. Let's dive deeper into each of these techniques and understand how they work.

Related Article: How to Check If a String is in an Enum in TypeScript

Merging Two Objects in TypeScript

Merging two objects in TypeScript involves combining the properties of two objects into a new object. This can be achieved using the spread operator (

...
...) or the
Object.assign
Object.assign method.

Here's an example of merging two objects using the spread operator:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const obj1 = { foo: 1, bar: 2 };
const obj2 = { baz: 3 };
const merged = { ...obj1, ...obj2 };
console.log(merged); // Output: { foo: 1, bar: 2, baz: 3 }
const obj1 = { foo: 1, bar: 2 }; const obj2 = { baz: 3 }; const merged = { ...obj1, ...obj2 }; console.log(merged); // Output: { foo: 1, bar: 2, baz: 3 }
const obj1 = { foo: 1, bar: 2 };
const obj2 = { baz: 3 };

const merged = { ...obj1, ...obj2 };

console.log(merged); // Output: { foo: 1, bar: 2, baz: 3 }

In this example, the spread operator is used to merge the properties of

obj1
obj1 and
obj2
obj2 into the
merged
merged object.

Alternatively, you can use the

Object.assign
Object.assign method to achieve the same result:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const obj1 = { foo: 1, bar: 2 };
const obj2 = { baz: 3 };
const merged = Object.assign({}, obj1, obj2);
console.log(merged); // Output: { foo: 1, bar: 2, baz: 3 }
const obj1 = { foo: 1, bar: 2 }; const obj2 = { baz: 3 }; const merged = Object.assign({}, obj1, obj2); console.log(merged); // Output: { foo: 1, bar: 2, baz: 3 }
const obj1 = { foo: 1, bar: 2 };
const obj2 = { baz: 3 };

const merged = Object.assign({}, obj1, obj2);

console.log(merged); // Output: { foo: 1, bar: 2, baz: 3 }

Both approaches result in a new object that contains the properties from both

obj1
obj1 and
obj2
obj2.

Merging Properties of Objects in TypeScript

Merging properties of objects in TypeScript involves updating or adding properties to an existing object. This can be achieved using the spread operator or the

Object.assign
Object.assign method.

Here's an example of merging properties of objects using the spread operator:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const obj1 = { foo: 1, bar: 2 };
const obj2 = { bar: 3, baz: 4 };
const merged = { ...obj1, ...obj2 };
console.log(merged); // Output: { foo: 1, bar: 3, baz: 4 }
const obj1 = { foo: 1, bar: 2 }; const obj2 = { bar: 3, baz: 4 }; const merged = { ...obj1, ...obj2 }; console.log(merged); // Output: { foo: 1, bar: 3, baz: 4 }
const obj1 = { foo: 1, bar: 2 };
const obj2 = { bar: 3, baz: 4 };

const merged = { ...obj1, ...obj2 };

console.log(merged); // Output: { foo: 1, bar: 3, baz: 4 }

In this example, the

bar
bar property of
obj1
obj1 is overwritten with the
bar
bar property of
obj2
obj2 in the resulting
merged
merged object.

Alternatively, you can use the

Object.assign
Object.assign method to achieve the same result:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const obj1 = { foo: 1, bar: 2 };
const obj2 = { bar: 3, baz: 4 };
const merged = Object.assign({}, obj1, obj2);
console.log(merged); // Output: { foo: 1, bar: 3, baz: 4 }
const obj1 = { foo: 1, bar: 2 }; const obj2 = { bar: 3, baz: 4 }; const merged = Object.assign({}, obj1, obj2); console.log(merged); // Output: { foo: 1, bar: 3, baz: 4 }
const obj1 = { foo: 1, bar: 2 };
const obj2 = { bar: 3, baz: 4 };

const merged = Object.assign({}, obj1, obj2);

console.log(merged); // Output: { foo: 1, bar: 3, baz: 4 }

Both approaches result in a new object that contains the merged properties from

obj1
obj1 and
obj2
obj2.

Object Spreading in TypeScript

Object spreading in TypeScript refers to spreading the properties of an object into another object or function call. This is achieved using the spread operator.

Here's an example of object spreading in TypeScript:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const obj1 = { foo: 1, bar: 2 };
const obj2 = { ...obj1, baz: 3 };
console.log(obj2); // Output: { foo: 1, bar: 2, baz: 3 }
const obj1 = { foo: 1, bar: 2 }; const obj2 = { ...obj1, baz: 3 }; console.log(obj2); // Output: { foo: 1, bar: 2, baz: 3 }
const obj1 = { foo: 1, bar: 2 };
const obj2 = { ...obj1, baz: 3 };

console.log(obj2); // Output: { foo: 1, bar: 2, baz: 3 }

In this example, the properties of

obj1
obj1 are spread into the object literal for
obj2
obj2, resulting in a new object that contains the properties from both objects.

Object spreading can also be used with function calls:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const obj = { foo: 1, bar: 2 };
function someFunction({ foo, bar }: { foo: number, bar: number }) {
console.log(foo, bar);
}
someFunction({ ...obj }); // Output: 1, 2
const obj = { foo: 1, bar: 2 }; function someFunction({ foo, bar }: { foo: number, bar: number }) { console.log(foo, bar); } someFunction({ ...obj }); // Output: 1, 2
const obj = { foo: 1, bar: 2 };

function someFunction({ foo, bar }: { foo: number, bar: number }) {
  console.log(foo, bar);
}

someFunction({ ...obj }); // Output: 1, 2

In this example, the properties of

obj
obj are spread into the argument for the
someFunction
someFunction call, allowing the function to receive the properties as separate arguments.

Related Article: How to Implement ETL Processes with TypeScript

Using Object Assign to Merge Objects in TypeScript

The

Object.assign
Object.assign method in TypeScript can be used to merge objects by copying the values of all enumerable properties from one or more source objects to a target object.

Here's an example of using

Object.assign
Object.assign to merge objects in TypeScript:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const obj1 = { foo: 1 };
const obj2 = { bar: 2 };
const obj3 = { baz: 3 };
const merged = Object.assign({}, obj1, obj2, obj3);
console.log(merged); // Output: { foo: 1, bar: 2, baz: 3 }
const obj1 = { foo: 1 }; const obj2 = { bar: 2 }; const obj3 = { baz: 3 }; const merged = Object.assign({}, obj1, obj2, obj3); console.log(merged); // Output: { foo: 1, bar: 2, baz: 3 }
const obj1 = { foo: 1 };
const obj2 = { bar: 2 };
const obj3 = { baz: 3 };

const merged = Object.assign({}, obj1, obj2, obj3);

console.log(merged); // Output: { foo: 1, bar: 2, baz: 3 }

In this example, the

Object.assign
Object.assign method is used to merge the properties of
obj1
obj1,
obj2
obj2, and
obj3
obj3 into the
merged
merged object.

Merging Objects with Same Keys in TypeScript

When merging objects in TypeScript that have the same keys, the resulting object will have the values of the last object with the same key.

Here's an example of merging objects with the same keys in TypeScript:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const obj1 = { foo: 1, bar: 2 };
const obj2 = { bar: 3, baz: 4 };
const merged = { ...obj1, ...obj2 };
console.log(merged); // Output: { foo: 1, bar: 3, baz: 4 }
const obj1 = { foo: 1, bar: 2 }; const obj2 = { bar: 3, baz: 4 }; const merged = { ...obj1, ...obj2 }; console.log(merged); // Output: { foo: 1, bar: 3, baz: 4 }
const obj1 = { foo: 1, bar: 2 };
const obj2 = { bar: 3, baz: 4 };

const merged = { ...obj1, ...obj2 };

console.log(merged); // Output: { foo: 1, bar: 3, baz: 4 }

In this example, the

bar
bar property of
obj1
obj1 is overwritten with the
bar
bar property of
obj2
obj2 in the resulting
merged
merged object.

Merging Objects with Different Keys in TypeScript

When merging objects in TypeScript that have different keys, the resulting object will contain the properties from both objects.

Here's an example of merging objects with different keys in TypeScript:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const obj1 = { foo: 1, bar: 2 };
const obj2 = { baz: 3 };
const merged = { ...obj1, ...obj2 };
console.log(merged); // Output: { foo: 1, bar: 2, baz: 3 }
const obj1 = { foo: 1, bar: 2 }; const obj2 = { baz: 3 }; const merged = { ...obj1, ...obj2 }; console.log(merged); // Output: { foo: 1, bar: 2, baz: 3 }
const obj1 = { foo: 1, bar: 2 };
const obj2 = { baz: 3 };

const merged = { ...obj1, ...obj2 };

console.log(merged); // Output: { foo: 1, bar: 2, baz: 3 }

In this example, the resulting

merged
merged object contains the properties from both
obj1
obj1 and
obj2
obj2.

Merging Nested Objects in TypeScript

Merging nested objects in TypeScript involves merging the properties of objects that are nested within other objects. This can be achieved using the spread operator or the

Object.assign
Object.assign method.

Here's an example of merging nested objects using the spread operator:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const obj1 = { foo: { bar: 1 } };
const obj2 = { foo: { baz: 2 } };
const merged = { ...obj1, ...obj2 };
console.log(merged); // Output: { foo: { baz: 2 } }
const obj1 = { foo: { bar: 1 } }; const obj2 = { foo: { baz: 2 } }; const merged = { ...obj1, ...obj2 }; console.log(merged); // Output: { foo: { baz: 2 } }
const obj1 = { foo: { bar: 1 } };
const obj2 = { foo: { baz: 2 } };

const merged = { ...obj1, ...obj2 };

console.log(merged); // Output: { foo: { baz: 2 } }

In this example, the

foo
foo property of
obj1
obj1 is overwritten with the
foo
foo property of
obj2
obj2 in the resulting
merged
merged object.

Alternatively, you can use the

Object.assign
Object.assign method to achieve the same result:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const obj1 = { foo: { bar: 1 } };
const obj2 = { foo: { baz: 2 } };
const merged = Object.assign({}, obj1, obj2);
console.log(merged); // Output: { foo: { baz: 2 } }
const obj1 = { foo: { bar: 1 } }; const obj2 = { foo: { baz: 2 } }; const merged = Object.assign({}, obj1, obj2); console.log(merged); // Output: { foo: { baz: 2 } }
const obj1 = { foo: { bar: 1 } };
const obj2 = { foo: { baz: 2 } };

const merged = Object.assign({}, obj1, obj2);

console.log(merged); // Output: { foo: { baz: 2 } }

Both approaches result in a new object that contains the merged properties of the nested objects.

Related Article: Tutorial: Loading YAML Files in TypeScript

Limitations and Caveats of Merging Objects in TypeScript

When merging objects in TypeScript, it's important to consider the limitations and caveats associated with each technique. Here are some common limitations and caveats to keep in mind:

- Merging objects with circular references can lead to infinite recursion and cause a stack overflow error.

- Merging objects with non-enumerable properties using

Object.assign
Object.assign will not include those properties in the resulting merged object.

- Merging nested objects can result in unexpected behavior if the objects have conflicting properties with the same keys.

It's important to thoroughly test and understand the implications of merging objects in TypeScript to avoid unexpected issues in your code.

External Sources

- TypeScript Documentation - Merging Objects

- MDN Web Docs - Object.assign()

- MDN Web Docs - Spread Syntax

You May Also Like

How to Implement and Use Generics in Typescript

Generics in TypeScript provide a powerful way to write reusable and type-safe code. This tutorial will guide you through the syntax, concepts, and pr… read more

How to Use the MouseEventHandlers in TypeScript

Learn how to work with the MouseEventHandlers in TypeScript in this tutorial. The article covers topics such as event handling, event listeners, and … read more

Tutorial: Using React-Toastify with TypeScript

This article provides a step-by-step guide on using React-Toastify with TypeScript. From setting up a TypeScript project to customizing toast notific… read more

Tutorial: Readonly vs Const in TypeScript

A detailed comparison of Readonly and Const in TypeScript. This article explores the Readonly and Const keywords in TypeScript, highlighting their di… read more

How to Update Variables & Properties in TypeScript

Updating variables and properties in TypeScript can be a simple and process. This step-by-step tutorial will guide you through the correct usage of t… read more

Comparing Go with TypeScript

An objective analysis of Go and TypeScript, their strengths and weaknesses. The article explores topics such as static typing, type safety, type anno… read more

Tutorial: Working with Dynamic Object Keys in TypeScript

Working with dynamic object keys in TypeScript can be a complex task, but with this step-by-step guide, you'll learn how to manipulate them effective… read more

Tutorial: Converting Boolean to String in TypeScript

Boolean values are a fundamental part of programming, and there may be times when you need to convert them to string format in TypeScript. In this tu… read more

How to Run Typescript Ts-Node in Databases

Running Typescript Ts-Node in databases can be a powerful tool for handling data in your applications. This tutorial provides a comprehensive guide o… read more

Tutorial on Exact Type in TypeScript

TypeScript is a powerful programming language that introduces static typing to JavaScript. In this tutorial, we will delve into the exact type featur… read more