zod: Field becomes optional when value is any or unknown

const s = z.object({ x: z.unknown() })
type s = z.infer<typeof s>
// s has { x?: unknown } but should be { x: unknown }

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 15
  • Comments: 20 (6 by maintainers)

Most upvoted comments

How does one write anything but something must be passed in and it cannot be undefined?

This is the best I’ve got:

const schema = z.object({
    field: z.custom((x) => x !== undefined),
  });

{ a: unknown } and { a?: unknwon} is different

type TestType = {
  test: unknown
}

const testValue: TestType = { test: 'value' }

const TestSchema = z.object({
  test: z.unknown(),
});

const parsedTestValue: TestType = TestSchema.parse(testValue);

Error Type '{ test?: unknown; }' is not assignable to type 'TestType'. Property 'test' is optional in type '{ test?: unknown; }' but required in type 'TestType'.

I’ve just added a PR which pretty much does exactly that (makes the shape an empty object and set the object to “passthrough”. Its added as the type z.anyObject().

Isn’t that the purpose of z.record?

Possible today:

z.record(z.unknown()) // don't care about key type.

Proposal:

z.record() // don't care about anything.

Intuitively, when writing typescript, you’d use Record<string, any> or Record<string, unknown> for such use case.

How does one write anything but something must be passed in and it cannot be undefined?

I’d say this is intentional for the moment. Currently Zod can’t really differentiate between {arg: string | undefined} and {arg?: string | undefined}. When you pass a key schema S s.t. undefined extends infer<typeof S>, Zod will add the question mark. In this case undefined extends unknown (because everything does), hence the question mark. This also agrees with runtime behavior:

const schema = z.object({ x: z.unknown() });
schema.parse({}); // works

The broader question of how/whether to provide an API that differentiates between different types of optionality is tracked here: https://github.com/colinhacks/zod/issues/635

What does it infer to? I’m almost certain it will infer to {}, which is the same as unknown and this will not correctly resemble empty objects. Actually, everything that extends an object will pass this from the inference point of view.

It infers to object - which is a standard TypeScript type for object and makes sense in this situation.

These type assertions seem to be correct to me: image

In the code, I’ve made sure the “output” type is object in this case- so yea, its not quite the same as passing in {} and making it "passthrough"- there was a little more involved.

I came up to this object schema definition

export type ZodObjectIdentity<T extends z.ZodRawShape, Side extends '_input' | '_output'> = {
  [K in keyof T]: T[K][Side];
};

export function zodObjectIdentity<T extends z.ZodRawShape>(
  t: T,
): z.ZodObject<T, 'strip', z.ZodTypeAny, ZodObjectIdentity<T, '_output'>, ZodObjectIdentity<T, '_input'>> {
  // @ts-ignore
  return z.object(t);
}

It removes question marks magic, so you can use it like this

const s = zodObjectIdentity({ x: z.unknown() })
type s = z.infer<typeof s>
//^? { x: unknown }