I'm first using z.infer
to produce types from Zod schemas and then Typedoc to generate documentation for those types, but the output is less than ideal in a few ways:
TypeName: z.infer<typeof schemaName>
, which isn't helpful in itself (thankfully schemaName
is clickable)ZodObject<{ name: ZodOptional<ZodString>}>
instead of the expected { name?: string }
name: undefined | string
rather than name?: string
or even (optional) name?: string
, which Typedoc does for regular (non-Zod) types.Here's an example (from repro repo):
index.ts
import z from 'zod'
export const userSchema = z.object({
name: z.string().optional()
})
export type ZodUser = z.infer<typeof userSchema>
export type TypescriptUser = {
name?: string
}
Once I run Typedoc on this, it's fairly obvious that the results are better with TypescriptUser than with ZodUser:
file:///…/zod-typedoc-ugly/docs/types/ZodUser.html:
ZodUser: z.infer<typeof userSchema>
file:///…/zod-typedoc-ugly/docs/variables/userSchema.html:
userSchema: ZodObject<{
name: ZodOptional<ZodString>;
}, "strip", ZodTypeAny, {
name: undefined | string;
}, {
name: undefined | string;
}> = ...
VS
file:///…/zod-typedoc-ugly/docs/types/TypescriptUser.html:
TypescriptUser: {
name?: string;
}
I'm looking for a way to get the advantages of Zod (I can use the schema responsible for the type at runtime to validate inputs) along with the simpler docs of zod-less Typescript types. I understand I can maintain the two separately, but I would rather that be automatic and not introduce a risk for both of these versions to fall out of sync.
Is there a better way, a workaround or an alternative here?
As of approximately 5 minutes ago, yes. There is a way. typedoc-plugin-zod can be used to make TypeDoc use the inferred type instead of the type node when converting the z.infer
type aliases. It will also replace the original schema declaration's type to refer to the ZodUser
type.
(obvious disclaimer, I just wrote this)