Search code examples
typescriptzod

How to write a tuple with spread type in Zod


In TypeScript, you would write:

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

How would you define that type definition using Zod?

Playground


Solution

  • 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