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

[TypeScript] [form] FormInstance 中的 validate 方法类型错误 #16776

Open
WangJincheng4869 opened this issue May 7, 2024 · 7 comments · Fixed by #16778
Open

[TypeScript] [form] FormInstance 中的 validate 方法类型错误 #16776

WangJincheng4869 opened this issue May 7, 2024 · 7 comments · Fixed by #16778

Comments

@WangJincheng4869
Copy link

Bug Type: TypeScript

Environment

  • Vue Version: 3.4.27
  • Element Plus Version: 2.7.2
  • Browser / OS: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36
  • Build Tool: Vite

Reproduction

Related Component

  • el-form

Reproduction Link

Github Repo

Steps to reproduce

官方文档 demo 中的代码直接复制后运行 tsc 就会报错,详见提供的复现链接中的 ElFormType.vue 文件

运行 pnpm type-check 会出现以下错误

图片

What is Expected?

不报错

What is actually happening?

报错

Additional comments

(empty)

@Liao-js
Copy link
Contributor

Liao-js commented May 7, 2024

你可以试下在 validate 里面不进行 return 或者 return 的类型是 promise,因为对 validatecallback 的返回类型定义是 Promise<void> | void

@warmthsea
Copy link
Contributor

warmthsea commented May 7, 2024

About PR:
#16652
#16669

@WangJincheng4869
Copy link
Author

@Liao-js 如果这里的写法发生变动,请增加非兼容版本号,并明显标注,如果全部修改是存在大量工作量的。经过测试 2.7.1 之前是没有问题的,这个严格验证应该是 2.7.2 新增的。个人认为这种不兼容的改动应该升级版本号为 2.8.0 并进行显著标记。

@yangyunfu-ht
Copy link

image

@btea
Copy link
Collaborator

btea commented May 12, 2024

ts里面即使函数标注返回类型为void,当函数作为参数时,即使函数返回其他类型也不会报错,所以之前即使返回 false ts也没有报错。

2.7.2返回类型新增了一个Promise<void>,在返回 false 时ts报错了。按照预期,只是新增了一种兼容情况,应该不会影响原来的逻辑,但事实是在这种情况下 ts 报错了。并非故意更改而不进行显著标记,而是不清楚会出现这种问题。

针对以上提到的情况,这里有一个简单的例子可以复现。链接

不过,目前我没有找到ts关于这种情况的详细说明,如果你们有更多详细说明信息,欢迎分享共同学习。

事实上,这里的返回值没有用处,所以 @warmthsea 创建了 PR 修改了官网示例,我将会合并他的 PR,但不会关闭这个 issue。

@btea btea reopened this May 12, 2024
@Liao-js
Copy link
Contributor

Liao-js commented May 12, 2024

我在 ts 看到这个问题的 issue,里面有项目是改回了 () => void 来避免报错

@btea
Copy link
Collaborator

btea commented May 12, 2024

@Liao-js 谢谢你提供的链接。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants