can1357/oh-my-pi · error · OmpTypeError

cannot apply partial to ${schema.ir.k}

Error message

cannot apply partial to ${schema.ir.k}

What it means

`.partial()` makes every property of an object schema optional (Zod's `.partial()` semantics); it throws when called on a non-object schema. The message interpolates the actual IR kind. Thrown from `partial()` at packages/omptype/src/zod.ts:241.

Source

Thrown at packages/omptype/src/zod.ts:241

					if (!(result instanceof type.errors)) return result;
				} catch {
					// A caught schema is deliberately total, including user refinement/transform exceptions.
				}
				return typeof fallback === "function" ? (fallback as () => Out)() : fallback;
			});
			return decorate(caught as Decoratable<Out>, optional);
		},
		strict(): ZodLikeSchema<Out> {
			return withObjectExtras("reject");
		},
		passthrough(): ZodLikeSchema<Out & Record<string, unknown>> {
			return withObjectExtras("keep") as ZodLikeSchema<Out & Record<string, unknown>>;
		},
		strip(): ZodLikeSchema<Out> {
			return withObjectExtras("delete");
		},
		partial(): Out extends object ? ZodLikeSchema<Partial<Out>> : ZodLikeSchema<Out> {
			if (schema.ir.k !== "object") throw new OmpTypeError(`cannot apply partial to ${schema.ir.k}`);
			const props = schema.ir.props.map(prop => ({ ...prop, opt: true }));
			return next(restrictBase(schema, { ...schema.ir, props })) as Out extends object
				? ZodLikeSchema<Partial<Out>>
				: ZodLikeSchema<Out>;
		},
	}) as unknown as ZodLikeSchema<Out>;
}

function decorateUnknown(schema: Decoratable<unknown>): ZodLikeSchema<unknown> {
	return decorate(schema);
}

export type infer<T> = T extends { readonly _output: infer Out } ? Out : never;

type SchemaOutput<Schema> = Schema extends { readonly _output: infer Out } ? Out : never;
type Shape = Readonly<Record<string, ZodLikeSchema<unknown>>>;
type ObjectOutput<S extends Shape> = {
	-readonly [K in keyof S as S[K] extends OptionalSchemaMarker ? never : K]: SchemaOutput<S[K]>;

View on GitHub (pinned to 9690622007)

Solutions

  1. Call .partial() only on schemas built with t.object({...})
  2. For update payloads, ensure the base schema is the object schema before calling .partial()
  3. For arrays/records, there is no .partial() equivalent — model optionality in the element/value schema instead

Example fix

// before
const patchSchema = t.array(t.string()).partial();
// after
const patchSchema = t.object({ name: t.string(), age: t.number() }).partial();
Defensive patterns

Strategy: type-guard

Validate before calling

function canApplyPartial(schema: ZodLikeSchema<unknown>): boolean {
  return schema.ir.k === "object";
}
if (!canApplyPartial(baseSchema)) throw new Error("partial() requires an object schema");

Type guard

function isObjectSchema(ir: { k: string }): ir is { k: "object" } {
  return ir.k === "object";
}

Try / catch

try {
  patchSchema = baseSchema.partial();
} catch (err) {
  if (err instanceof OmpTypeError && err.message.startsWith("cannot apply partial")) {
    throw new Error("base schema must be t.object({...}) for .partial()");
  } else throw err;
}

Prevention

When it happens

Trigger: `t.string().partial()`, `t.array(...).partial()`, or `.partial()` on schemas produced by unions/primitives — anywhere `schema.ir.k !== "object"`.

Common situations: Building PATCH-style update schemas where the base type was changed from an object to a union or wrapper; calling .partial() on the wrong variable in a chain; assuming .partial() works on records/arrays like it does in some other libraries.

Related errors


AI-assisted analysis of can1357/oh-my-pi@9690622007 (2026-08-31). Data as JSON: /api/errors/1495e9653760d44d. Report an issue: GitHub.