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

onSuccess方法的入参data无法获取正确的type #31

Closed
joker77877 opened this issue Mar 16, 2021 · 15 comments
Closed

onSuccess方法的入参data无法获取正确的type #31

joker77877 opened this issue Mar 16, 2021 · 15 comments
Labels
enhancement New feature or request ts typescript problem

Comments

@joker77877
Copy link

Bug 描述 Bug description

onSuccess方法的入参data无法获取正确的type

代码重现 Reproduce

531615877457_ pic_hd
541615877516_ pic_hd
551615877742_ pic

期望结果 Desired result

onSuccess方法的入参data可以获取正确的type

其他信息 Other information

@github-actions
Copy link

Hello @joker77877. Please provide a online reproduction by forking this link or a minimal GitHub repository. Make sure to choose the correct version.
你好 @joker77877, 我们需要你提供一个在线的重现实例以便于我们帮你排查问题。你可以通过点击 此处 创建一个 codesandbox 或者提供一个最小化的 GitHub 仓库。请确保选择准确的版本。

@John60676 John60676 added bug Something isn't working ts typescript problem and removed Need Reproduce labels Mar 16, 2021
@John60676
Copy link
Member

onSuccess 的类型推断有点问题。

临时解决方案: 可以手动给 useRequest 提供泛型,代码如下

    const { data } = useRequest<string, any, number>(testService, {
      formatResult: res => Number(res),
      onSuccess: data => {
        // `data` type is number
      },
    });

@joker77877
Copy link
Author

这样可以但是我要再次写一次类型,希望还是完善一下类型的推断。
561615954748_ pic
再问下这里的data设置了initialData依旧有undefined的类型

@John60676
Copy link
Member

问题一:这样可以但是我要再次写一次类型,希望还是完善一下类型的推断。

目前出现这个问题只会在 formatResultonSuccess 同时使用的情况下。看了一下还没有完美的解决方案。后续可能先会把 onSuccess 的 data 入参设置成 any,等找到方法再进行修复。

其实按照你提供的代码截图,你这段代码可以不使用 onSuccess 来处理的,直接用 watch 去监听 data 即可

问题二:data设置了initialData依旧有undefined的类型

这是 ts 的问题。如果你能确保 data.value 不为 undefined 的话,你可以加个 非空断言,即 data.value!。后续再考虑一下看看是否需要把 dataerrorundefined 移除

@John60676 John60676 added enhancement New feature or request and removed bug Something isn't working labels Mar 17, 2021
@joker77877
Copy link
Author

好的谢谢你的解答

@John60676
Copy link
Member

这 issue 挂着吧。免得我忘了😆

@John60676 John60676 reopened this Mar 18, 2021
John60676 added a commit that referenced this issue Mar 22, 2021
@John60676
Copy link
Member

已发布:1.0.5

在 formatResult 和 onSuccess 同时使用的情况下,onSuccess 的 data 入参设置成 any,如果想要 onSuccess 有类型提示的,可以显式传入第三个泛型, 参考

    const { data } = useRequest<string, any, number>(testService, {
      formatResult: res => Number(res),
      onSuccess: data => {
        // `data` type is number
      },
    });

上面提到的改动仅在 formatResult 和 onSuccess 同时使用,且没显式提供泛型 的情况下,其他用法均不受影响

@joker77877
Copy link
Author

我更新为1.0.5之后还是有点问题
581616483963_ pic_hd
我没有给useRequest传入三个泛型,但是我的getDetail确实已经将泛型传入useRequest,并且指向onSuccess也上图所示已经显示data的类型,但是当我指向data时他依然是any,是一定要显式传入第三个泛型data才能显示正确类型吗。
image

@John60676
Copy link
Member

是的,这个行为确实很怪异,鼠标移上去时他是能把正确类型推断出来的,但是实际使用时却是没有正确的类型,这个目前也没有很好的解决方案。至于 any 这是我们内部做了处理。如果内部不把 onSuccess 的入参类型改成any,让其自动推断的话,会导致 resultValue 里面的 data 丢失类型。所以为了确保 resultValue 的 data 类型正确,我们再 1.0.5 版本暂时以这个方案修复了

其实看你截图的代码,你这个需求貌似可以用 vue 的 watch 来实现

@joker77877
Copy link
Author

是的,我这个需求用watch实现了,但是只有onSuccess的时候data类型就正确。

@John60676
Copy link
Member

John60676 commented Mar 23, 2021

是的,我这个需求用watch实现了,但是只有onSuccess的时候data类型就正确。

1.0.5 做了以下的修改

在 formatResult 和 onSuccess 同时使用的情况下,onSuccess 的 data 入参设置成 any,如果想要 onSuccess 有类型提示的,可以显式传入第三个泛型。

单独使用 onSuccess 是不受影响的,自动推断也是正常的。

看看后续有没有好的解决方案吧。如果你有解决方法的话,也欢迎提 PR 帮我们解决这个问题😁

@joker77877
Copy link
Author

image
defaultParams和onSuccess同时使用报错 没有与此调用匹配的重载

@John60676
Copy link
Member

没能重现哦。提供个能重现的可运行示例看看

@joker77877
Copy link
Author

onSuccess函数中加入参数就会出现这个ts报错

@John60676
Copy link
Member

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request ts typescript problem
Projects
None yet
Development

No branches or pull requests

2 participants