redux-saga + typescript 使用 takeLatest 报错

代码

import { takeLatest, put, select } from 'redux-saga/effects'
import { GET_COMMENT_LIST } from './actionTypes'
import { fetch } from '../../../common/index'
import { initCommentList } from './actionCreators'

interface IAction {
  commentType?: string,
  page?: number | string
}

function* axiosCommentList({ commentType, page }: IAction) {
  const user = yield select((state: { user: { user: string } }) => state.user.user)

  try {
    const formData = {
      type: commentType,
      page: page,
      pageSize: 10,
      create_user: user,
      to_user: user
    }
    const {
      data: {
        data: { result, total }
      }
    } = yield fetch.post('/api/comment/getusercommentlist', formData)
    yield put(initCommentList(result, total))
  } catch (e) {
    console.log(e.message)
  }
}

export function* getCommentList() {
  yield takeLatest(GET_COMMENT_LIST, axiosCommentList)
}

takeLatest 会报错
image.png

No overload matches this call.  
The last overload gave the following error.  
类型“string”的参数不能赋给类型“TakeableChannel<unknown>”的参数。

但是我把 IAction 改下就不会报错了

interface IAction {
  commentType?: string,
  page?: number | string,
  [x: string]: any
}

=.=有没有大佬遇到过

阅读 3.7k
1 个回答

解决了

import { Action } from 'redux'
...
interface IState extends Action {
  commentType: string,
  page: number | string
}
...

IState 继承下官方 Action 即可

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
logo
Microsoft
子站问答
访问
宣传栏