API reference@evolu/commonSet › isNonEmptySet

function isNonEmptySet<T>(set: ReadonlySet<T>): set is NonEmptyReadonlySet<T>;

Defined in: packages/common/src/Set.ts:108

Checks if a set is non-empty and narrows its type to NonEmptyReadonlySet.

Both mutable and readonly sets narrow to the branded NonEmptyReadonlySet type, which can be used with functions like firstInSet.

To check if a set is empty, use if (!isNonEmptySet(set)) — using the negated guard is better than .size === 0 for early returns because TypeScript narrows the type after the check.

Example

const set: ReadonlySet<number> = new Set([1, 2, 3]);
if (isNonEmptySet(set)) {
  firstInSet(set); // set is NonEmptyReadonlySet<number>
}