Javascript 财产';iFetching';不存在于类型';绝不';但在对象扩散时有效

Javascript 财产';iFetching';不存在于类型';绝不';但在对象扩散时有效,javascript,typescript,ecmascript-6,redux,Javascript,Typescript,Ecmascript 6,Redux,为什么我会得到这个类型脚本错误属性'isFetching'在类型'never'上不存在。ts(2339)用于下面的代码段 const mapStateToProps = ( state: AppState, ownProps: AuthenticationPageProps ): IMapStateToProps => ({ isFetching: state.authentication.isFetching, user: state.authentication.use

为什么我会得到这个类型脚本错误
属性'isFetching'在类型'never'上不存在。ts(2339)
用于下面的代码段

const mapStateToProps = (
  state: AppState,
  ownProps: AuthenticationPageProps
): IMapStateToProps => ({
  isFetching: state.authentication.isFetching,
  user: state.authentication.user
});
但是如果我把物体散开,它会像预期的那样工作吗

const mapStateToProps = (
  state: AppState,
  ownProps: AuthenticationPageProps
): IMapStateToProps => {
  const { isFetching, user } = state.authentication;
  return { isFetching, user };
};
此代码段也可以正常工作:

const mapStateToProps = (
  state: AppState,
  ownProps: AuthenticationPageProps
): IMapStateToProps => ({
  isFetching: state.authentication["isFetching"],
  user: state.authentication["user"],
});
下面是我在打印
props

interface IMapStateToProps {
  isFetching: boolean;
  user: User;
}

这是
IMapStateToProps

interface IMapStateToProps {
  isFetching: boolean;
  user: User;
}
更多接口

export interface AuthenticationState {
  isFetching: boolean;
  user: User;
  errors: Error[];
}

export interface User {
  email?: string;
}

export interface Error {
  message: string;
  status: string;
}

export const rootReducer = combineReducers({
  authentication: authenticationReducer, // authenticationReducer returns type: AuthenticationState
});

export type AppState = ReturnType<typeof rootReducer>;

初始状态与返回状态的类型不同

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: {},
});
导出接口身份验证状态{
iFetching:布尔值;
user:user |{};//注意联合类型
错误:错误[];
}
导出常量AuthenticationReducerDefaultState={
错误:[],
isFetching:false,
用户:{},
};
//还应冻结状态,因为您不希望意外突变。
export const AuthenticationReducerDefaultState=Object.freeze({
错误:[],
isFetching:false,
用户:{},
});

你能发布AppState类型吗。@RaduDițăupdated!如果我忘了什么,请告诉我。你能把你的身份证明贴出来吗?