Created
June 15, 2022 02:56
-
-
Save Tanimodori/70a7025d4199a36895d22fe7fea52dd4 to your computer and use it in GitHub Desktop.
How to use generic types in Zod
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { z } from 'zod'; | |
// The zod model with generics type in function form | |
const PaginatedList = <T extends z.ZodTypeAny>(elemType: T) => | |
z.object({ | |
// some properties omitted | |
url: z.string(), | |
data: z.array(elemType), | |
}); | |
// The "dynamic type" of model "PaginatedList" | |
// Which extends z.ZodTypeAny | |
// The generic param T here is dynamic type | |
// Note: require typescript@^4.7 for instantiation expressions | |
type PaginatedListModel<T extends z.ZodTypeAny> = ReturnType<typeof PaginatedList<T>>; | |
// The "static type" of model "PaginatedList" | |
// Which is like z.infer<z.ZodTypeAny> | |
// The generic param T here is static type | |
type PaginatedList<T> = z.infer<PaginatedListModel<z.ZodType<T>>>; | |
/* | |
= { | |
url: string; | |
data: T[]; | |
} | |
*/ | |
// The parser | |
const definePaginatedListParser = | |
<T extends z.ZodTypeAny>(zodModel: PaginatedListModel<T>) => | |
(data: z.infer<typeof zodModel>): PaginatedList<T> => { | |
return zodModel.parse(data); | |
}; | |
// Using verified data in codebase | |
const extractData = (list: PaginatedList<string>): string[] => { | |
return list.data; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Original code from colinhacks/zod#670