redux 在 Saga 调用效果中没有与此调用匹配的重载

tp5buhyn  于 2022-11-12  发布在  其他
关注(0)|答案(2)|浏览(207)

我想传递action.url字符串作为返回Promise的topicDummy函数的参数,但它一直显示我

No overload matches this call.
  The last overload gave the following error.
    Argument of type '<TopicData>(url: string) => Promise<TopicData>' is not assignable to parameter of type '{ context: unknown; fn: (this: unknown, ...args: any[]) => any; }'.
      Type '<TopicData>(url: string) => Promise<TopicData>' is missing the following properties from type '{ context: unknown; fn: (this: unknown, ...args: any[]) => any; }': context, fnts(2769)
effects.d.ts(499, 17): The last overload is declared here.

这是我的全部密码

export type TopicData = {
  name: TopicName
  data: CardData[]
}
const dummyTopicData: TopicData = Object.assign({
  topic: 'etc',
  data: dummyData()
}, )
function topicDummy<TopicData>(url: string): Promise<TopicData> {
  return new Promise((resolve, reject) => {
    setTimeout(() => 
      dummyTopicData 
    , 700);  
  })   
}

function* fetchTopic(action: DispatchAction){
  try{
    yield put(topicCreator.load()); 
    const topicList = yield call(topicDummy, action.url); // <- complain here. 
    yield put(topicCreator.success(topicList));
  } catch(error){
    throw new Error(`Error exist in fetchTopic function`); 
  }
}
e7arh2l6

e7arh2l61#

How to repair a 'TS2769: No overload matches this call'可能是相关的,
它建议,而不是

import {call} from "redux-saga/effects";

以便使用

import * as Effects from "redux-saga/effects";

const call: any = Effects.call;

也可以看看https://github.com/redux-saga/redux-saga/issues/2018
您还需要在某个点上解决该承诺,并且如果要使用这些数据,您需要“* 返回 *”这些数据。
所以改变

return new Promise((resolve, reject) => {
    setTimeout(() => 
      dummyTopicData 
    , 700);  
  })

return new Promise((resolve, reject) => {
    setTimeout(() => 
      resolve(dummyTopicData)
    , 700);  
  })
w7t8yxp5

w7t8yxp52#

我想我也遇到了同样的问题,我在我的案例中找到了解决办法。
我有这样的代码:

const {dashboardId} = yield call(
      createProgramDashboard,
      programId,
      name,
      type
    );

我在createProgramDashboard下看到一条红色的弯曲线,并显示以下错误:

No overload matches this call.
  The last overload gave the following error.
    Argument of type '(programId: string, name: string, type: REPORT_TYPE) => Promise<Dictionary>' is not assignable to parameter of type '{ context: unknown; fn: (this: unknown, ...args: any[]) => any; }'.
      Type '(programId: string, name: string, type: REPORT_TYPE) => Promise<Dictionary<any>>' is missing the following properties from type '{ context: unknown; fn: (this: unknown, ...args: any[]) => any; }': context, fnts(2769)
effects.d.ts(499, 17): The last overload is declared here.

结果是,我发送的第一个参数programIdnumber,而createProgramDashboard期望的是string
显然,当发送给call函数的一些参数的类型(在我的例子中为programId: number)与被调用的函数(在我的例子中为createProgramDashboard)所期望的参数的类型(在我的例子中为programId: string)不匹配时,就会显示此错误。
因此,这修复了错误:

const {dashboardId} = yield call(
      createProgramDashboard,
      programId.toString(),
      name,
      type
    );

相关问题