英文:
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: "",
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);
I am still getting Type 'Form' is not assignable to type 'ToRefs<{ code: any; quantity: any; amount: any; expiresAt: any; }>
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 '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);
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:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论