Home > Enterprise >  How to write a tuple with spread type in Zod
How to write a tuple with spread type in Zod

Time:12-16

In TypeScript, you would write:

type A = ["A", ...string[]];

How would you define that type definition using Zod?

Playground

CodePudding user response:

According to the zod documentation on tuples:

A variadic ("rest") argument can be added with the .rest method.

so a schema for your use case would look like:

const schema = z.tuple([z.literal('A')]).rest(z.string())

Playground

  • Related