如何修复在redux-saga中调用takeEvery()时发生的TypeScript错误?

时间:2018-04-16 07:43:11

标签: reactjs typescript redux redux-saga

import { createAction, handleActions } from 'redux-actions';
import { delay } from 'redux-saga';
import { all, put, takeEvery } from 'redux-saga/effects';

export const action_creators = {
    get_action_of_set_number: createAction<{ number:number }>('set_number'),
    get_action_of_set_number_async: createAction<{ number:number }>('set_number_async')
};

const state = {
    number: 0
}

function* set_number_async(action: { payload: { number:number } }) {
    yield delay(1000);
    yield put(action_creators.get_action_of_set_number(action.payload));
}

export function* saga(action) {
    yield all([takeEvery('set_number_async', set_number_async)]);
    *** Error message : Argument of type '"set_number_async"' is not assignable
    *** to parameter of type 'Channel<{ payload: { number:number; }; }>'.
}

export default handleActions({
    set_number: (state, action) => {
        const new_number = action.payload!.number;
        return { ...state, number:new_number };
    }
}, state);

完整示例:https://codesandbox.io/s/zry4vl2ymx(为方便起见,省略了其他类型定义。)

我不知道自己做错了什么。如果你知道什么是问题以及如何解决它,请帮助解决问题。谢谢! :)

1 个答案:

答案 0 :(得分:1)

当我遇到这个问题时,是因为我的传奇故事实际上并没有采取行动,因为我认为您遇到了同样的事情。

def win(self): sent_by_bot = self.sender in self.bots is_giveaway = ('giveaway' in self.msg.lower()) is_win = ('won' in self.msg.lower()) is_me = ('AAAAA' in self.msg.lower()) if sent_by_bot and is_giveaway and is_win and is_me: return True return false 中,方法takeEvery('set_number_async', set_number_async)的参数没有type属性,这意味着它的形状不像Action类型。 (请记住,打字稿中的类型是鸭子/结构式的,它们只在乎类型具有的属性。)

如果我通过添加type属性来更改签名,则示例将编译:

set_number_async