Eliya Cohen
Eliya Cohen

Reputation: 11478

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

Upvotes: 6

Views: 3615

Answers (1)

Souperman
Souperman

Reputation: 9836

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

Upvotes: 6

Related Questions