John John - 1 month ago 19
TypeScript Question

what is the ... syntax in typescript

Jumping from regular old js with ang1 to ang2 with typescript and redux etc is a heck of a steep climb.

Could anyone offer a simple explanation to the ... syntax? Having come from a php and javascript background this is really new stuff.

http://blog.ng-book.com/introduction-to-redux-with-typescript-and-angular-2/#deleting-an-item-without-mutation shows this as an example with redux:

return {
messages: [
...state.messages.slice(0, idx),
...state.messages.slice(idx + 1, state.messages.length)
]


But does anyone have an uber simple example for a complete noob to this area? Feels like i'm going round in circles!

Answer

The three dots represent the rest operator and it is used to get the arguments list passed to function on invocation and in array destructure. A case when the operator gathers the rest remained after the operation.

The spread operator is used for array construction and destructuring, and to fill function arguments from an array on invocation. A case when the operator spreads the array (or iterable object) elements.

You can read more about this here.