首页 > 解决方案 > 如何修复错误“参数'state'和'state'的类型不兼容”

问题描述

我最近将我的 react native 项目传递给了打字稿,但我遇到了这个错误,但我不知道如何修复它。错误是: Types of parameters 'state' and 'state' are incompatible 在此处输入图像描述

这是身份验证减速器:


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;

预先感谢您的帮助 !

标签: typescriptreact-native

解决方案


问题是关于ActionReducer.

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

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


推荐阅读