alovajs / alova

Workflow-Streamlined next-generation request tools. Extremely streamline API integration workflow, just one step
https://alova.js.org
MIT License
2.96k stars 108 forks source link

在ts项目中use hook handler返回的数据类型不一致会报错 #518

Closed JOU-amjs closed 2 months ago

JOU-amjs commented 2 months ago

这是否是一个 Bug?

这个问题是否已经存在?

Alova 版本

3.0.12

前端框架

其他框架

问题描述

image

这个代码报了以下错误

Argument of type '() => Method<RespondedAlovaGenerics<AlovaGenerics<any, any, AlovaXHRRequestConfig, AlovaXHRResponse<any>, AlovaXHRResponseHeaders, AlovaDefaultCacheAdapter, AlovaDefaultCacheAdapter, { ...; }>, string, unknown>> | Method<...>' is not assignable to parameter of type 'Method<RespondedAlovaGenerics<AlovaGenerics<any, any, AlovaXHRRequestConfig, AlovaXHRResponse<any>, AlovaXHRResponseHeaders, AlovaDefaultCacheAdapter, AlovaDefaultCacheAdapter, { ...; }>, string, unknown>> | AlovaMethodHandler<...>'.
  Type '() => Method<RespondedAlovaGenerics<AlovaGenerics<any, any, AlovaXHRRequestConfig, AlovaXHRResponse<any>, AlovaXHRResponseHeaders, AlovaDefaultCacheAdapter, AlovaDefaultCacheAdapter, { ...; }>, string, unknown>> | Method<...>' is not assignable to type 'AlovaMethodHandler<RespondedAlovaGenerics<AlovaGenerics<any, any, AlovaXHRRequestConfig, AlovaXHRResponse<any>, AlovaXHRResponseHeaders, AlovaDefaultCacheAdapter, AlovaDefaultCacheAdapter, { ...; }>, string, unknown>>'.
Type 'Method<RespondedAlovaGenerics<AlovaGenerics<any, any, AlovaXHRRequestConfig, AlovaXHRResponse<any>, AlovaXHRResponseHeaders, AlovaDefaultCacheAdapter, AlovaDefaultCacheAdapter, { ...; }>, string, unknown>> | Method<...>' is not assignable to type 'Method<RespondedAlovaGenerics<AlovaGenerics<any, any, AlovaXHRRequestConfig, AlovaXHRResponse<any>, AlovaXHRResponseHeaders, AlovaDefaultCacheAdapter, AlovaDefaultCacheAdapter, { ...; }>, string, unknown>>'.
      Type 'Method<RespondedAlovaGenerics<AlovaGenerics<any, any, AlovaXHRRequestConfig, AlovaXHRResponse<any>, AlovaXHRResponseHeaders, AlovaDefaultCacheAdapter, AlovaDefaultCacheAdapter, { ...; }>, number, unknown>>' is not assignable to type 'Method<RespondedAlovaGenerics<AlovaGenerics<any, any, AlovaXHRRequestConfig, AlovaXHRResponse<any>, AlovaXHRResponseHeaders, AlovaDefaultCacheAdapter, AlovaDefaultCacheAdapter, { ...; }>, string, unknown>>'.
        Type 'RespondedAlovaGenerics<AlovaGenerics<any, any, AlovaXHRRequestConfig, AlovaXHRResponse<any>, AlovaXHRResponseHeaders, AlovaDefaultCacheAdapter, AlovaDefaultCacheAdapter, { ...; }>, number, unknown>' is not assignable to type 'RespondedAlovaGenerics<AlovaGenerics<any, any, AlovaXHRRequestConfig, AlovaXHRResponse<any>, AlovaXHRResponseHeaders, AlovaDefaultCacheAdapter, AlovaDefaultCacheAdapter, { ...; }>, string, unknown>'.
          Type 'RespondedAlovaGenerics<AlovaGenerics<any, any, AlovaXHRRequestConfig, AlovaXHRResponse<any>, AlovaXHRResponseHeaders, AlovaDefaultCacheAdapter, AlovaDefaultCacheAdapter, { ...; }>, number, unknown>' is not assignable to type '{ Responded: string; Transformed: unknown; }'.
            Types of property 'Responded' are incompatible.
              Type 'number' is not assignable to type 'string'.ts-plugin(2345)

期望的表现

不报错

复现链接

No response

复现步骤

系统信息

No response

补充说明

No response

JOU-amjs commented 2 months ago

这个问题的原因是返回了多个Method类型导致,例如Method<AG1> | Method<AG2> | ...,但use hooks内部的类型推断只能来自于一个Method类型。

解决方案是,你可以手动将Method<AG1> | Method<AG2>转换为Method<AG1 | AG2 | ...>即可,例如:

useRequest(() => {
  const m1 = alova.Get<string>('/xxx');
  const m2 = alova.Get<number>('/xxx');
  return (!api.abort ? m1 : m2) as Method<(typeof m1 extends Method<infer AG1> ? AG1 : never) | (typeof m2 extends Method<infer AG2> ? AG2 : never)>
})