Type 'Form' is not assignable to type 'ToRefs<{ code: any; quantity: any; amount: any; expiresAt: any; }>'

huangapple go评论85阅读模式
英文:

Type 'Form' is not assignable to type 'ToRefs<{ code: any; quantity: any; amount: any; expiresAt: any; }>'

问题

在当前的Vue3 Composition API文件中的设置中:

type Form = {
  code: string;
  amount: number | null;
  quantity: number | null;
  checked: boolean;
  expiresAt?: Date;
};

const form: Form = reactive({
  code: "",
  quantity: null,
  amount: null,
  checked: false,
  expiresAt: new Date(),
});

const rules = {
  code: {
    required,
    minLength: minLength(2),
  },
  quantity: {
    required,
  },
  amount: {
    required,
    minValue: minValue(1),
  },
  expiresAt: {
    required: requiredIf(() => !form.checked),
  },
};

const v$ = useVuelidate(rules, form);

在v$行上仍然出现 Type 'Form' is not assignable to type 'ToRefs<{ code: any; quantity: any; amount: any; expiresAt: any; }>' 的错误。您可以将规则对象更改为以下方式以使 vuelidate 理解类型:

const rules = ref({
  code: {
    required,
    minLength: minLength(2),
  },
  quantity: {
    required,
  },
  amount: {
    required,
    minValue: minValue(1),
  },
  expiresAt: {
    required: requiredIf(() => !form.checked),
  },
});
const v$ = useVuelidate(rules, form);

通过将rules包装在ref中,您可以解决类型不匹配的问题。

英文:

With the current setup in a Vue3 Composition API file:

type Form = {
  code: string;
  amount: number | null;
  quantity: number | null;
  checked: boolean;
  expiresAt?: Date;
};

const form: Form = reactive({
  code: &quot;&quot;,
  quantity: null,
  amount: null,
  checked: false,
  expiresAt: new Date(),
});

const rules = {
  code: {
    required,
    minLength: minLength(2),
  },
  quantity: {
    required,
  },
  amount: {
    required,
    minValue: minValue(1),
  },
  expiresAt: {
    required: requiredIf(() =&gt; !form.checked),
  },
};

const v$ = useVuelidate(rules, form);

I am still getting Type &#39;Form&#39; is not assignable to type &#39;ToRefs&lt;{ code: any; quantity: any; amount: any; expiresAt: any; }&gt; on the v$ line. What can I add to the rules object to make vuelidate understand the types?

答案1

得分: 1

import ref from 'vue'

type Form = {
  code: string;
  amount: number | null;
  quantity: number | null;
  checked: boolean;
  expiresAt?: Date;
};

const form: Form = ref({
  code: "",
  quantity: null,
  amount: null,
  checked: false,
  expiresAt: new Date(),
});

const rules = {
  code: {
    required,
    minLength: minLength(2),
  },
  quantity: {
    required,
  },
  amount: {
    required,
    minValue: minValue(1),
  },
  expiresAt: {
    required: requiredIf(() => !form.checked),
  },
};

const v$ = useVuelidate(rules, form);

根据你的要求,这是代码的翻译部分。不包含其他内容。

英文:
import ref from &#39;vue&#39;

    type Form = {
      code: string;
      amount: number | null;
      quantity: number | null;
      checked: boolean;
      expiresAt?: Date;
    };
    
    const form: Form = ref({
      code: &quot;&quot;,
      quantity: null,
      amount: null,
      checked: false,
      expiresAt: new Date(),
    });
    
    const rules = {
      code: {
        required,
        minLength: minLength(2),
      },
      quantity: {
        required,
      },
      amount: {
        required,
        minValue: minValue(1),
      },
      expiresAt: {
        required: requiredIf(() =&gt; !form.checked),
      },
    };
    
    const v$ = useVuelidate(rules, form);

First post here, could you try with this ? According to your mistake, Ref should be used instead of reactive.

Here's a link explaining the difference between the two:

https://vuejsdevelopers.com/2022/06/01/ref-vs-reactive/

huangapple
  • 本文由 发表于 2023年6月5日 18:06:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76405335.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定