英文:
Inertia.js & Typescript: Argument type {} & InertiaFormProps{} is not assignable to parameter type Partial<VisitOptions> | undefined
问题
I created an empty Laravel project and installed Laravel Breeze with Typescript support.
Now when I create a form (with useForm()
) and I use the .post()
method and I use one of the options (e.g. onFinish: () =>
) I get the following error:
> Argument type {onFinish: () => {password: string, password_confirmation: string, name: string, email: string} & InertiaFormProps<{password: string, password_confirmation: string, name: string, email: string}>} is not assignable to parameter type Partial
When I hover it in my editor it suggests to add all the members. Does this have something to do with Typescript or am I doing something wrong?
英文:
I created an empty Laravel project and installed Laravel Breeze with Typescript support.
Now when I create a form (with useForm()
) and I use the .post()
method and I use one of the options (e.g. onFinish: () =>
) I get the following error:
> Argument type {onFinish: () => {password: string,
> password_confirmation: string, name: string, email: string} &
> InertiaFormProps<{password: string, password_confirmation: string,
> name: string, email: string}>} is not assignable to parameter type
> Partial<VisitOptions> | undefined
When I hover it in my editor it suggests to add all the members. Does this has something to do with Typescript or am I doing something wrong?
答案1
得分: 2
需要使用{}
来调用箭头函数,使其形式为functionName: () => {...}
。你的代码形式为functionName: () => ...
。
在你的代码中,将onFinish
更改为:
onFinish() => { //注意这里有大括号
form.reset(...)
}
我的理解是,如果省略{}
,则箭头函数具有简洁体,它仅包含一个单一表达式,其结果将隐式成为函数的返回值,这个return
值可能导致问题。
但说实话,我不确定为什么你的不起作用,而使用{}
起作用。
英文:
You need to call the arrow function with a {}
so it's of the form functionName: () => {...}
. Your code is of the form functionName: () => ...
In your code, change onFinish
to
onFinish() => { //note the curly here
form.reset(...)
}
My understanding is that if you omit the {} then the arrow function has a concise body, which consists solely of a single expression whose result will implicitly become the return value of the function, and this return
value may be causing the issue.
But, tbh, I'm not sure why yours doesn't work but the {}
does.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论