API reference › @evolu/common › Set › addToSet
function addToSet<T>(set: ReadonlySet<T>, item: T): NonEmptyReadonlySet<T>;
Defined in: packages/common/src/Set.ts:165
Returns a new readonly set with an item added.
If the item already exists, returns a new set with the same elements (still a new reference for change detection).
Adding without mutation
import {
addToSet,
assertEqual,
assertTrue,
assertType,
type NonEmptyReadonlySet,
} from "@evolu/common";
const original: ReadonlySet<number> = new Set([1, 2]);
const added = addToSet(original, 3);
const unchanged = addToSet(original, 2);
assertType<NonEmptyReadonlySet<number>, typeof added>();
assertEqual(added, new Set([1, 2, 3]));
assertTrue(unchanged !== original);