Skip to main content

patch

patch

Merges an object of type T with updates of type Partial<T>. Returns a new object where the updates override the original values and does not mutate the original one.

Immutability is explained in the immutability & serializable state concept.

Example

interface Creature {
id: number;
type: string;
name: string;
}

const cat = { id: 1, type: 'cat' };

const catWithName = patch(cat, { name: 'Fluffy' });

// catWithName will be:
// {id: 1, type: 'cat', name: 'Fluffy'};

Example — with rxState()

import { Component } from '@angular/core';
import { Subject } from 'rxjs';
import { rxState } from '@rx-angular/state';
import { patch } from '@rx-angular/cdk/transformations';

@Component({
/* ... */
})
export class ProfileComponent {
readonly changeName$ = new Subject<string>();

private readonly state = rxState<ComponentState>(({ connect }) => {
connect(this.changeName$, (state, name) => patch(state, { name }));
});

// Imperative alternative
changeName(name: string): void {
this.state.set(patch(this.state.get(), { name }));
}
}

Example — signals-first

import { signal } from '@angular/core';
import { patch } from '@rx-angular/cdk/transformations';

const profile = signal<Profile>({ id: 1, type: 'cat' });

// patch is the central helper for immutable object updates on a signal
profile.update((state) => patch(state, { name: 'Fluffy' }));

Edge cases

patch({}, state) > state;
patch(null as any, state) > state;
patch(state, null as any) > state;
patch(null as any, null as any) > null;
patch(undefined as any, undefined as any) > undefined;
patch(state, nonObject) > state;
patch(nonObject, state) > state;
patch(nonObject, nonObjectUpdate) > nonObject;

Signature

function patch<T extends object>(object: T, upd: Partial<T>): T;

Parameters

object

typeof: T

upd

typeof: Partial<T>