Open mauroviniciussilva opened 2 weeks ago
Hi @mauroviniciussilva that's correct, I looked into the documentation and found .input
.
To work around this and get the input type (before defaults are applied), you can use z.input<typeof Schema>
instead of z.infer
. This will preserve the optionality of nickname, so it’s typed as string | undefined
, matching what you need for your generic function.
This is what your code would look like:
import { z } from 'zod';
const Schema = z.object({
id: z.number(),
name: z.string(),
nickname: z.string().optional().default('')
});
type SchemaInput = z.input<typeof Schema>; // nickname will be string | undefined
const parsedSchema: z.ZodType<SchemaInput> = Schema;
I'm trying to use a type inferred from a zod schema, but when I need to reuse the inferred type in other generic function I just couldn't find a way of doing it.
Considering the following code:
I got the following error:
I can understand that the inferred type assumed that, once I have the default
''
the field wouldn't be undefined in the generated type:But, if I need to use the generated type to force the usage of a Schema that is compatible with a type, I cannot use the inferred type to accomplish my goal.
In the documentation there's no information on how to infer a type without considering the
default
to be able to reuse the inferred type in other generic functions. Is this a relevant functionality to the package?