-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Typescript error when typing submit handler function #3521
Comments
This came up before in #3510 Since there is no concept of generic components in TypeScript for Vue.js (not directly), the accurate type for the submitted For that case, const { handleSubmit } = useForm<{ name: string; }>();
const onSubmit = handleSubmit(values => {
values.name; // string
}); I could expose a hack that would allow you to create a typed version of the |
Is there a full example anywhere? Thanks |
For anyone looking for example, I solved it using
|
if you are using yup, I made a helper function to get the submit function with types from a yup schema function getSubmitFn<Schema extends Yup.ObjectSchema<Record<string, any>>>(
_: Schema,
callback: (values: Yup.InferType<Schema>) => void
) {
return (values: Record<string, any>) => {
return callback(values);
};
} const schema = Yup.object().shape({
emailId: Yup.string().email().required().label("Email Address"),
password: Yup.string().required().label("Password").min(8),
});
const submit = getSubmitFn(schema, (values) => {
console.log(values.emailId);
console.log(values.password) // you get types from the schema here
}); // submit callback only gets called after validation <Form @submit="submit" :validation-schema="schema">
///
</Form> |
Using the code from @SamroodAli I came up with this for zod schemas also adding the ctx so we can use the actions inside the function: function getSubmitFn<Schema extends z.ZodObject<Record<string, any>>>(
_: Schema,
callback: (values: z.infer<Schema>, ctx: SubmissionContext) => void
) {
return (values: Record<string, any>, ctx: SubmissionContext): void => {
callback(values, ctx);
};
} export const schema = z.object({
email: z
.string({
required_error: "Email is required",
invalid_type_error: "Email must be a string",
})
.email(),
password: z.string({
required_error: "Password is required",
invalid_type_error: "Password must be a string",
}),
});
const validationSchema = toFormValidator(schema);
const submit = getSubmitFn(schema, (values, ctx) => {
console.log(values.email);
console.log(ctx);
}) <Form @submit="submit" :validation-schema="validationSchema">
///
</Form> |
Now since you can create generic components in Vue 3.3 - is this possible? |
@logaretm The official document does not explain how to use the generic with |
You can't. The |
In case someone stumbles across here. I'm using yup and this is what worked for me: Placing the schema into a variable, note that this is not yet For regular typing use Then I pass the In my submit function, I can then infer the type: This is described in the docs of yup, but it took me a while to understand it: |
What happened?
On the latest version
4.5.x
we can no longer do this:Typescript errors out with:
This seems to be because before the
@submit
had the typeand now it has the type:
Reproduction steps
1- Provide a typed function to
@submit
Version
Vue.js 3.x and vee-validate 4.x
What browsers are you seeing the problem on?
Relevant log output
Demo link
https://github.com/slauzinho/vee-validate-typescript-error/blob/master/src/App.vue
Code of Conduct
The text was updated successfully, but these errors were encountered: