Skip to content
Sarj Code Standards

no-redundant-optional-array-default

Remove .optional() immediately inside a Zod array default.

Why

An outer default already accepts undefined array input, so the inner optional wrapper adds no state or fallback behavior.

Fix

Keep the array default and remove the immediately preceding .optional() call.

Examples

Before — flagged Do not wrap a defaulted list in a redundant optional
src/schema.ts
import { z } from "zod";
const Items = z.array(z.string()).optional().default([]);
After — preferred Default an omitted list directly
src/schema.ts
import { z } from "zod";
const Items = z.array(z.string()).default([]);
Before — flagged Official Zod subpaths retain the finding for manual review
src/schema.ts
import { array, string } from "zod/v4";
const Items = array(string()).optional().default([]);
After — preferred Preserve an unrelated builder in a directory named zod
src/my-zod/index.ts
export const z = {
string: () => "string",
array: (_element: string) => ({
optional: () => ({
default: (value: readonly string[]) => ({ value, optional: true }),
}),
default: (value: readonly string[]) => ({ value, optional: false }),
}),
};
src/schema.ts
import { z } from "./my-zod/index.js";
export const Items = z.array(z.string()).optional().default([]);