@rbxts/jsnatives



Object.patch

Applies a diff object to transform the target object according to the changes specified in the diff.

Signature

function patch<T, U, Deep extends boolean>(current: T, diff: U, deep?: Deep): T

Description

The Object.patch() method applies a diff object (typically created by Object.diff()) to transform the target object according to the changes specified in the diff. This is useful for applying incremental updates to objects, particularly in state management and synchronization scenarios.

The patch operation is designed to work with the diff operation in a way that patch(other, diff(current, other)) will transform other to match current.

Examples

Basic patching

const current = { a: 1, b: 2, c: 3 };
const other = { a: 1, b: 4, d: 5 };
const diff = Object.diff(current, other);
const result = Object.patch(other, diff);
print(result); // { a: 1, b: 2, c: 3 }

Deep patching

const current = {
user: {
name: "John",
age: 30
}
};
const other = {
user: {
name: "John",
age: 31
}
};
const diff = Object.diff(current, other, true);
const result = Object.patch(other, diff, true);
print(result); // { user: { name: "John", age: 30 } }

Notes