Languages
[Edit]
EN

TypeScript - spread operator (...)

0 points
Created by:
maciek211
425

In this article, we would like to show you how works spread operator in TypeScript.

The spread operator allows you to spread the iterated value into its components.

Usage examples:

1. Breaking an array into individual elements:

Edit

Output:

2. Copying an array:

Edit

Output:

3. Combine arrays:

Edit

Output:
 

4. Math object. 

Edit

Functions like Math.max() expect many parameters. We can't pass the array as one argument there, so we can use the spread operator.

Output:

5. Spread operator with objects

Edit

Example 1

Edit

Note:

As we can see above, the order does matter - y value from object1 has been overwritten by value from object2.

Example 2

Edit

Example 3

Edit

Example 4

Edit

Alternative titles

  1. TypeScript - Spread syntax (...)
  2. TypeScript - three dots syntax
  3. TypeScript - three dots operator
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join