Inertia.js & Typescript: Argument type {} & InertiaFormProps{} is not assignable to parameter type Partial<VisitOptions> | undefined

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

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 | undefined

Inertia.js & Typescript: Argument type {} & InertiaFormProps{} 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 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: () =&gt;) 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

Inertia.js & Typescript: Argument type {} & InertiaFormProps{} 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: () =&gt; {...}。你的代码形式为functionName: () =&gt; ...

在你的代码中,将onFinish更改为:

onFinish() =&gt; {   //注意这里有大括号
     form.reset(...)
}

我的理解是,如果省略{},则箭头函数具有简洁体,它仅包含一个单一表达式,其结果将隐式成为函数的返回值,这个return值可能导致问题。

但说实话,我不确定为什么你的不起作用,而使用{}起作用。

英文:

You need to call the arrow function with a {} so it's of the form functionName: () =&gt; {...}. Your code is of the form functionName: () =&gt; ...

In your code, change onFinish to

onFinish() =&gt; {   //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.

huangapple
  • 本文由 发表于 2023年5月24日 20:47:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76323706.html
匿名

发表评论

匿名网友

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

确定