类型“"MY_EVENTS_LOAD"”的参数不可分配给 yeild takeLatest 中类型 'TakeableChannel<unknown>' 的参数

Argument of type '"MY_EVENTS_LOAD"' is not assignable to parameter of type 'TakeableChannel<unknown>' in yeild takeLatest

我在打字稿上有一个错误,我正在使用 Redux 和 Saga 作为中间件,这些是错误:

No overload matches this call. The last overload gave the following error. Argument of type '"MY_EVENTS_LOAD"' is not assignable to parameter of type 'TakeableChannel'.

我想创建一个观察者,在这里:

export default function* watcher() {
  yield takeEvery(actions.MY_EVENTS_LOAD, getListEventsByUserSaga);
}

并且在我的 saga 文件中有函数

export function* getListEventsByUserSaga(OwnerId: string) {
  try {
    const events = yield getEventsByOwnerId(OwnerId);
    yield put(actions.fetchMyEventsListUserSuccess(events));
  } catch (error) {
    yield put(actions.fetchMyEventsListUserFailure(error.message));
  }
}

当我执行操作时会发生这种情况:

export const fetchMyEventsListUserLoad = (OwnerId: string) => {
  return {
    type: MY_EVENTS_LOAD,
    OwnerId,
  };
};

怎样才能正确的实现这些?

在您的 getListEventsByUserSaga 生成器中,您将操作声明为 {OwnerId:string} 而没有类型。如果您尝试用类型声明它,这将消除错误。您可以使用 ReturnType.

export function* getListEventsByUserSaga({OwnerId} : ReturnType<typeof fetchMyEventsListUserLoad>) {
  try {
    // code
  } catch (error) {
    // code
  }
}

https://github.com/redux-saga/redux-saga/issues/1883

您可以做的(至少我在我的案例中所做的)是 this link 中指出的。目前,您似乎只将 OwnerId 字符串输出到生成器,这不应该像现在这样工作,因为那里的生成器基本上接受分派的参数,一个具有类型的对象和发送到的其余参数调度员。 "correct" 的方法是拥有一个包含类型的类型定义,例如:

type Params = { OwnerId: string, type: string }
export function* getListEventsByUserSaga({ OwnerId }: Params) {
...
}

而不是

export function* getListEventsByUserSaga(action: { payload: any; }) {
...
//logic
...
}

使用这个

export function* getListEventsByUserSaga(action: any) {
...
//logic
...
}

对我有用