Giter Club home page Giter Club logo

Comments (15)

github-actions avatar github-actions commented on September 2, 2024

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 仓库。请确保选择准确的版本。

from vue-request.

John60676 avatar John60676 commented on September 2, 2024

onSuccess 的类型推断有点问题。

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

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

from vue-request.

joker77877 avatar joker77877 commented on September 2, 2024

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

from vue-request.

John60676 avatar John60676 commented on September 2, 2024

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

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

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

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

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

from vue-request.

joker77877 avatar joker77877 commented on September 2, 2024

好的谢谢你的解答

from vue-request.

John60676 avatar John60676 commented on September 2, 2024

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

from vue-request.

John60676 avatar John60676 commented on September 2, 2024

已发布: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 同时使用,且没显式提供泛型 的情况下,其他用法均不受影响

from vue-request.

joker77877 avatar joker77877 commented on September 2, 2024

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

from vue-request.

John60676 avatar John60676 commented on September 2, 2024

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

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

from vue-request.

joker77877 avatar joker77877 commented on September 2, 2024

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

from vue-request.

John60676 avatar John60676 commented on September 2, 2024

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

1.0.5 做了以下的修改

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

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

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

from vue-request.

joker77877 avatar joker77877 commented on September 2, 2024

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

from vue-request.

John60676 avatar John60676 commented on September 2, 2024

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

from vue-request.

joker77877 avatar joker77877 commented on September 2, 2024

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

from vue-request.

John60676 avatar John60676 commented on September 2, 2024

image

from vue-request.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.