.parse() Typescript linter accepts anything (string, number...) #1892
Copy link
Copy link
Closed
Description
Hi,
I didn't find someone having my issue so I'm probably doing something wrong but... I can't figure the solution out.
Let's say I have:
const TestSchema = z.object({
email: z.string(),
});
TestSchema.parse('hello');The last line will not throw an error with the Typescript whereas when hovering the parse function I can see:
(method) ZodType<{ email: string; }, ZodObjectDef<{ email: ZodString; }, "strip", ZodTypeAny>, { email: string; }>.parse(data: unknown, params?: Partial<z.ParseParams> | undefined): {
email: string;
}Note that I use strict: true in my tsconfig.json (and at runtime, zod throws an error obviously).
A "workaround" would be to always do this:
const TestSchema = z.object({
email: z.string(),
});
type TestSchemaType = z.infer<typeof TestSchema>;
TestSchema.parse(<TestSchemaType>'hello');... but it's pretty verbose and annoying.
Any idea what's the issue to get the linter working normally?
Thank you,
EDIT: it seems a solution would be to use
const typedParse = <Schema extends z.ZodSchema> (
schema: Schema, data: z.infer<Schema>
): z.infer<Schema> => schema.parse( data )
typedParse(ATest, 'hello');(ref: #1748 (comment))
... but I don't get why this is not used by default?
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels
Activity
JacobWeisenburger commentedon Jan 16, 2023
What do you mean by 'normally'?
#1748 (comment)
Another way you can solve this problem:
Would this work for you?