Search code examples
typescriptecmascript-6ngrx

ngrx 8 createAction syntax


I am learning ngrx Angular now. I have little confusion with ngrx syntax.

I know that the below Syntax of arrow function simply return "customer" object.

export const addCustomer = createAction(

   '[Customer] Add Customer',
     (customer: Customer) => {customer}
);

But what is this Syntax? What does this below round brackets "({customer})" mean? Am I missing something of ES6?

export const addCustomer = createAction(

   '[Customer] Add Customer',
     (customer: Customer) => ({customer})
);

Solution

  • (customer: Customer) => ({customer}) is a function which returns an object { customer: customer }.

    To understand it you need to be familiar with two concepts.

    1. () => ({}) is the same as () => { return {}; }.
    2. { customer } is the same as { customer: customer }. Typescript allows such shortcuts in case property and variable names coincide (e.g. customer key and customer variable coincide in our case).