Spread Operator In TypeScript
Photo: Burst By Shopify

Spread Operator In TypeScript

In Typescript, you can use spread operator to spread the element of an array or object. The spread operator can be used for initializing an array or object from another array or object. You can also use spread operator for object destructuring.

To read more about object destructuring, you can follow below link:

Let’s explore some of the examples of Spread operators.

Spread Operator Examples

Learn how to initialize array from another array

Create new array from existing array.

Expanded version of an array into another array

A Better way to concatenate arrays

Learn how to initialize object from another object

Similarly, you can create new object from existing object.

Spread operator in Object Destructuring

The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.

Object Spread

You can simply add a property to an object without mutating the original.

Note: For objects, the order of where you put the spread matters. What comes first is ‘overridden’ by what comes later.

Summary

So, in summary, we learn how we can use spread operator to spread an array or object elements. I tried to give a lot of useful example of using the spread operator in your TypeScript code. It is really fun to play with spread operator.

Further Reading

Adesh

Hi, I am Adesh. I am a senior software engineer having more than 12 years of software development experience. I am a full stack developer and interested in writing the technical post on programming. I am currently working in New York City area.

This Post Has 2 Comments

  1. Hi Adesh,
    It seems to me that example2.ts does not run.
    I’ve changed your code for this
    (myFunc)(…paramArray);
    So it works.
    Bye,
    Rob

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Close Menu