O'Reilly logo

TypeScript: Modern JavaScript Development by Ivo Gabe de Wolff, Vilic Vane, Remo H. Jansen

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating actions

An action should modify the state. To do that, we will first create three helper functions. One to modify a part of an object, one to modify a part of an array, and one to easily create a new array.

We use the same update function as we did in Chapter 3, Note-Taking App with a Server. We add this function to lib/model/utils.ts.

export function update<U extends V, V>(old: U, changes: V): U { 
  const result = Object.create(Object.getPrototypeOf(old)); 
  for (const key of Object.keys(old)) { 
    result[key] = (<any> old)[key]; 
  } 
  for (const key of Object.keys(changes)) { 
    result[key] = (<any> changes)[key]; 
  } 
  return result; 
} 

We also create a function that changes the element at a certain index of an array. The other elements will remain at ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required