Skip to content

Merging objects with Partial type

twitter logo github logo ・1 min read  

My usual implementation pattern of immutable merging in TypeScript.

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

    clone() {
        return new MyClass(,;

    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( === "1");
console.log( === "bar");

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

twitter logo DISCUSS
Classic DEV Post from Aug 24 '19

To Code, or Not to Code on Vacation: That is My Question

I coded on vacation, and now I feel guilty about it. What tips do you have?

Suguru Inatomi profile image
Japanese Web Front-end Dev / Google Developers Expert for Angular / Lead of Angular Japan User Group