typescript - 如何修复错误“参数'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;
预先感谢您的帮助 !
解决方案
问题是关于ActionReducer
.
export interface ActionReducer<T, V extends Action = Action> {
(state: T | undefined, action: V): T;
}
如您所见,它是T
或undefined
。但是,在您的减速器中,状态可能被定义为T
only。这会导致类型问题。
推荐阅读
- user-interface - 在遵守 FreeDesktop 标准的同时,在哪里为用户特定的应用程序安装图标?
- c# - 在 ASP.NET MVC 中更新多个选择下拉列表(选择 2)
- asp.net-web-api - Azure 托管服务出错“发送请求时出错”
- json - 如何从 JSON API 调用项目并添加到 TabBar - Flutter
- java - 为什么我的多线程程序会出现三种结果?
- google-drive-api - 无法使用邮递员在 Google Drive 中创建文件夹
- javascript - DataTable 中的 rowsPerPageOptions 未显示
- android - 我的 build.gradle 文件出现错误
- reactjs - 在 docker 容器中使用 React 部署 ExpressJS
- c# - 有没有办法使用谷歌应用脚本识别谷歌演示文稿中的隐藏幻灯片?