Skip to content
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

Keep generic aspect of SubmissionHandler in FormSlotProps #4845

Open
quentinboudinel opened this issue Aug 26, 2024 · 1 comment
Open

Keep generic aspect of SubmissionHandler in FormSlotProps #4845

quentinboudinel opened this issue Aug 26, 2024 · 1 comment
Labels
👕 TypeScript TypeScript typings issue

Comments

@quentinboudinel
Copy link

quentinboudinel commented Aug 26, 2024

Is your feature request related to a problem? Please describe.

In the following scenario:

<script setup lang="ts">
import type { MyGenericObject } from '@/types' // MyGenericObject extends GenericObject

function onSubmit(values: MyGenericObject) {}
</script>

<template>
  <Form v-slot="{ handleSubmit }">
    <form @submit.prevent="handleSubmit($event, onSubmit)"></form>
  </Form>
</template>

My type checker doesn't agree with me defining the values param of the onSubmit function param as anything else than GenericObject and this because FormSlotProps is not generic and its property handleSubmit is defined strictly as SubmissionHandler (with no generic params)

Describe the solution you'd like

I would like to be able to define my submission handler as previously described without type checker error.

Describe alternatives you've considered

I think redefining

type FormSlotProps = UnwrapRef<Pick<FormContext, 'meta' | 'errors' | 'errorBag' | 'values' | 'isSubmitting' | 'isValidating' | 'submitCount' | 'validate' | 'validateField' | 'handleReset' | 'setErrors' | 'setFieldError' | 'setFieldValue' | 'setValues' | 'setFieldTouched' | 'setTouched' | 'resetForm' | 'resetField' | 'controlledValues'>> & {
    handleSubmit: (evt: Event | SubmissionHandler, onSubmit?: SubmissionHandler) => Promise<unknown>;
    submitForm(evt?: Event): void;
    getValues<TValues extends GenericObject = GenericObject>(): TValues;
    getMeta<TValues extends GenericObject = GenericObject>(): FormMeta<TValues>;
    getErrors<TValues extends GenericObject = GenericObject>(): FormErrors<TValues>;
};

to include generic params should do the trick

@logaretm logaretm added the 👕 TypeScript TypeScript typings issue label Oct 19, 2024
@zumm
Copy link

zumm commented Dec 26, 2024

There is same problem with onSubmit event of Form because type is PropType<SubmissionHandler<GenericObject>>. It makes Form kinda useless in typescript:

<script setup lang="ts">
interface MyForm {}

const onSubmit: SubmissionHandler<MyForm> = (values) => {}
</script>

<template>
  <!-- type error here -->
  <Form @submit=onSubmit />
</template>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
👕 TypeScript TypeScript typings issue
Projects
None yet
Development

No branches or pull requests

3 participants