2017-11-16

Merging objects with Partial type

#typescript
This post is possibly out of date.

My usual implementation pattern of immutable merging in TypeScript.

class MyClass {
  constructor(public id: string, public name: string) {}

  clone() {
    return new MyClass(this.id, this.name);
  }

  merge(another: Partial<MyClass>) {
    return Object.assign(this.clone(), another);
  }
}

const objA = new MyClass("1", "foo");

const objB = objA.merge({ name: "bar" });

console.log(objA !== objB);
console.log(objB.id === "1");
console.log(objB.name === "bar");

Partial<MyClass> allows us to pass an object matching MyClass partially.

このエントリーをはてなブックマークに追加
comments powered by Disqus