首页 > 解决方案 > TS:没有类型的 Redux Actions

问题描述

我最近开始在我的 react 项目中使用 Typescript。我缺少的一件是键入调度和动作创建者。

我正在使用我自己的操作类型,基于:https ://redux.js.org/recipes/reducing-boilerplate

我的动作编码如下:

export function getXXX(){ 
  return {
    types: [ 
        ActionTypes.GET_XXX, 
        ActionTypes.GET_XXX_SUCCESS, 
        ActionTypes.GET_XXX_ERROR 
    ],
    request: {
        method: 'GET',
        url: `/v1/retrievexxx`
    }
  }    
}

我的问题是缺少类型(显然)并且 ReduxAction TS 类型需要一个类型。

有什么办法可以解决这个问题?

标签: reactjstypescriptredux

解决方案


I would really recommend not trying to work around this, and always provide a type for your actions. There is a reason why the Action type in the Redux typings requires it: big parts of the Redux ecosystem - such as the Dev Tools - expect it to be there, and will act weirdly if it's not. So it's probably not worth going against the grain here.


推荐阅读