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

Registering @vee-validate/rules in typescript produces typing errors #4766

Closed
5 tasks done
thany opened this issue May 31, 2024 · 1 comment
Closed
5 tasks done

Registering @vee-validate/rules in typescript produces typing errors #4766

thany opened this issue May 31, 2024 · 1 comment
Labels
👕 TypeScript TypeScript typings issue

Comments

@thany
Copy link

thany commented May 31, 2024

What happened?

There are typing errors when registering the rules from @vee-validate/rules into vee-validate:

import { defineRule } from 'vee-validate';
import * as rules from '@vee-validate/rules';

Object.entries(rules).forEach(([name, rule]) => {
  defineRule(name, rule); // <-- error happens at `rule`
});

The error:

Argument of type '((value: unknown, params: [string] | { locale?: string | undefined; }) => boolean) | ((value: unknown, params: [string] | { locale?: string | undefined; }) => boolean) | ((value: unknown, params: [string] | { ...; }) => boolean) | ... 24 more ... | ((value: unknown, params: [...] | { ...; }) => boolean)' is not assignable to parameter of type 'ValidationRuleFunction<string | RawFormSchema<unknown>, [string] | { locale?: string | undefined; }> | SimpleValidationRuleFunction<string | RawFormSchema<unknown>, [...] | { ...; }>'.
  Type 'SimpleValidationRuleFunction<unknown, BetweenParams>' is not assignable to type 'ValidationRuleFunction<string | RawFormSchema<unknown>, [string] | { locale?: string | undefined; }> | SimpleValidationRuleFunction<string | RawFormSchema<unknown>, [...] | { ...; }>'.
    Type 'SimpleValidationRuleFunction<unknown, BetweenParams>' is not assignable to type 'ValidationRuleFunction<string | RawFormSchema<unknown>, [string] | { locale?: string | undefined; }>'.
      Types of parameters 'params' and 'params' are incompatible.
        Type '[string] | { locale?: string | undefined; }' is not assignable to type 'BetweenParams'.
          Type '[string]' is not assignable to type 'BetweenParams'.
            Type '[string]' is not assignable to type '[string | number, string | number]'.
              Source has 1 element(s) but target requires 2.

Reproduction steps

  1. Install the latest vee-validate into a typescript project
  2. Put the above code, or the code from the example, in a .ts file somewhere in that project
  3. See error message, in VScode or while compiling

Version

Vue.js 3.x and vee-validate 4.x

What browsers are you seeing the problem on?

  • Firefox
  • Chrome
  • Safari
  • Microsoft Edge

Relevant log output

No response

Demo link

N/A

Code of Conduct

@logaretm logaretm added the 👕 TypeScript TypeScript typings issue label Jun 2, 2024
@logaretm
Copy link
Owner

logaretm commented Jun 3, 2024

The issue seems to be because toTypedSchema is exported as well which isn't a rule.

At the same time fixing that would result in a too complex type error due to the union and argument sizes being different.

I will export an all export that should be safe to use type-wise and update the docs accordingly.

Something like this:

import { all } from '@vee-validate/rules';

Object.entries(all).forEach(([name, rule]) => {
  defineRule(name, rule);
});

logaretm added a commit that referenced this issue Jun 3, 2024
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

2 participants