如何修复错误“参数类型'状态'和'状态'不兼容”

时间:2019-02-01 20:47:52

标签: typescript react-native

我最近将我的react本机项目传递给Typescript,但出现此错误,我找不到解决方法。 错误是: Types of parameters 'state' and 'state' are incompatible enter image description here

这是认证还原器:


const initialState = {
  credentials: {
    Username: '',
    Password: '',
  },
  LoggedIn: false,
};

function authentication(state = initialState, action: any) {
  switch (action.type) {
    case userConstants.LOGIN_REQUEST:
      return {
        credentials: action.credentials,
      };
    case userConstants.LOGIN_SUCCESS:
      return { state };
    default:
      return state;
  }
}

export default authentication;

预先感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

问题是关于 ActionReducer 的签名。

export interface ActionReducer<T, V extends Action = Action> {
    (state: T | undefined, action: V): T;
}

如您所见,它是 Tundefined。但是,在您的减速器中,状态可能仅定义为 T。这会导致类型问题。