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

Type error for @validate argument when using validateYup #188

Open
fastfedora opened this issue Aug 24, 2023 · 1 comment
Open

Type error for @validate argument when using validateYup #188

fastfedora opened this issue Aug 24, 2023 · 1 comment

Comments

@fastfedora
Copy link

When using this library in a Glint component, I am getting a type error trying to use validateYup with @validate:

import { validateYup } from 'ember-headless-form-yup';

...

export default class MyComponent {
  ...

  <template>
    <HeadlessForm
      @validate={{validateYup this.args.schema}}
      as |form|
    >
      ...
   </HeadlessForm>
  </template>
}

The error I'm getting is:

Conversion of type 'FormValidateCallback<Partial<DATA>>' to type 'FormValidateCallback<DATA>' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
  Type 'Partial<DATA>' is not comparable to type 'DATA'.
    'Partial<DATA>' is assignable to the constraint of type 'DATA', but 'DATA' could be instantiated with a different subtype of constraint 'object'.glint(2352)

The issue appears to be that validateYup returns FormValidateCallback<Partial<DATA>> while @validate accepts only FormValidateCallback<DATA>.

My current workaround is to move the code into a getter in the component and re-type it there:

  get validate(): FormValidateCallback<DATA> | undefined {
    return this.args.schema
      ? validateYup(this.args.schema) as unknown as FormValidateCallback<DATA>
      : undefined;
  }
@simonihmig
Copy link
Contributor

It might be related to the fact that you are not passing @data to the form, which is basically how the generic DATA type is inferred. Even when you don't pre-populate the form with data, it makes sense to pass a value to infer the type, even if that value is just an empty object, but defined to have the type that your form expects. Like in this test for example?

@fastfedora could you please try that out?

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

No branches or pull requests

2 participants