1 回答

TA貢獻1820條經(jīng)驗 獲得超10個贊
初始狀態(tài)與返回的狀態(tài)類型不同。
export interface AuthenticationState {
isFetching: boolean;
user: User;
errors: Error[];
}
export const AuthenticationReducerDefaultState = {
errors: [], //Type never
isFetching: false,
user: {}, //Type {} not type User
};
你可以通過以下方式解決這個問題:
export interface AuthenticationState {
isFetching: boolean;
user: User | {}; //Note the union type
errors: Error[];
}
export const AuthenticationReducerDefaultState = <AuthenticationState>{
errors: [],
isFetching: false,
user: {},
};
//Also should freeze the state as you would not want accidental mutations.
export const AuthenticationReducerDefaultState = Object.freeze<AuthenticationState>({
errors: [],
isFetching: false,
user: {},
});
添加回答
舉報