嘿,这里是打字稿的新手,尤其是 redux。如标题所述,我的商店中的初始状态会弹出问题。错误的顶部 - Argument of type '{ userLogin: { userInfo: any; }; }' is not assignable to parameter of type '{ userLogin?: undefined; }'。
店铺
import { createStore, combineReducers, applyMiddleware } from "redux";
import thunk from "redux-thunk";
import { composeWithDevTools } from "redux-devtools-extension";
import { userLoginReducer } from "./user/reducer";
export type RootState = ReturnType<typeof store.getState>;
const reducer = combineReducers({
userLogin: userLoginReducer,
});
const userInfoFromStorage = localStorage.getItem("userInfo")
? JSON.parse(localStorage.getItem("userInfo")!)
: null;
const initialState = {
userLogin: { userInfo: userInfoFromStorage },
};
const middleware = [thunk];
export const store = createStore(
reducer,
initialState,
composeWithDevTools(applyMiddleware(...middleware))
);
Argument of type '{ userLogin: { userInfo: any; }; }' is not assignable to parameter of type '{ userLogin?: undefined; }'.
Types of property 'userLogin' are incompatible.
Type '{ userInfo: any; }' is not assignable to type 'undefined'.ts(2345)
const initialState: {
userLogin: {
userInfo: any;
};
}
减速器
import { Store } from "redux";
import {
USER_LOGIN_REQUEST,
USER_LOGIN_SUCCESS,
USER_LOGIN_FAIL,
USER_LOGOUT,
UserActionTypes,
} from "./types";
export const userLoginReducer = (state: Store, action: UserActionTypes) => {
switch (action.type) {
case USER_LOGIN_REQUEST:
return { loading: true };
case USER_LOGIN_SUCCESS:
return { loading: false, userInfo: action.payload };
case USER_LOGIN_FAIL:
return { loading: false, error: action.payload };
case USER_LOGOUT:
return {};
default:
return state;
}
};
我写这个是因为我得到了大部分代码的错误