API reference › @evolu/common › Type › nonEmptyArray
function nonEmptyArray<
ParentType extends AnyType & {
Output: readonly unknown[];
},
>(
parent: ValidateBrandParent<"NonEmptyArray", ParentType>,
): Type<"NonEmptyArray">;
Defined in: packages/common/src/Type.ts:13306
Adds non-empty validation to an existing array Type.
Narrows the Output to a NonEmptyReadonlyArray while preserving the parent Type's constraints and brands, so functions that require a non-empty array accept it without another check. Validation leaves the array unchanged. For strings, use minLength.
Example
import {
assertEqual,
assertErr,
assertOk,
array,
firstInArray,
nonEmptyArray,
String,
} from "@evolu/common";
const Tags = nonEmptyArray(array(String));
const tags = Tags.fromUnknown(["local-first", "offline"]);
assertOk(tags, ["local-first", "offline"]);
assertEqual(firstInArray(tags.value), "local-first");
const invalid = Tags.fromUnknown([]);
assertErr(invalid, { type: "NonEmptyArray", value: [] });
assertEqual(
Tags.formatError(invalid.error),
"The value [] must contain at least one item.",
);