API reference › @evolu/common › Set › isNonEmptySet
function isNonEmptySet<T>(set: ReadonlySet<T>): set is NonEmptyReadonlySet<T>;
Defined in: packages/common/src/Set.ts:133
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.
Narrowing before access
import {
assertEqual,
assertType,
isNonEmptySet,
type NonEmptyReadonlySet,
} from "@evolu/common";
const set: ReadonlySet<number> = new Set([1, 2, 3]);
if (!isNonEmptySet(set)) throw new Error("Expected a non-empty set");
assertType<NonEmptyReadonlySet<number>, typeof set>();
assertEqual(set.size, 3);