Javascript 为Redux存储定义initialState的不同方法

Javascript 为Redux存储定义initialState的不同方法,javascript,reactjs,typescript,redux,react-redux,Javascript,Reactjs,Typescript,Redux,React Redux,我最近遇到了这个例子: 州检察官 export interface IState { token: string | null; } 这很有效。然而,我试图这样改变它: const initialState: IState = { token: null }; export const tokenReducer = ( //state: IState['token'] = null, state = initialState, //state: initialS

我最近遇到了这个例子:

州检察官

export interface IState {
    token: string | null;
  }

这很有效。然而,我试图这样改变它:

const initialState: IState = {
  token: null
};

export const tokenReducer = (
  //state: IState['token'] = null,
  state = initialState,
  //state: initialState
  action: AppAction,
): typeof state => {
  switch (type) {
    case 'LOGIN':
      return action.payload;
    default:
      return state;
  }
};
但我会犯错误。 如果根据IDE的建议使用
state:typeof initialState
state=initialState
,则action.payload出错:

Type 'string' is not assignable to type 'IState'.ts(2322)
如果我尝试
state:initialState
,显然:

'initialState' refers to a value, but is being used as a type here. Did you mean 'typeof initialState'?ts(2749)
``
What am I doing wrong? Am I making a syntax error or is it just not allowed to define initialStates like this?

问题可能是:你根本没有返回状态。您只是覆盖它或仅返回令牌(而不是作为对象),这取决于您的负载。我假设
LOGIN
-action将返回一个令牌。因此,正确的减速器如下所示:

export const tokenReducer=(
状态:IState['token']=初始状态,
{type,payload}:AppAction,
):IState=>{
开关(类型){
案例“登录”:
返回{
……国家,
令牌:有效载荷,
};
违约:
返回状态;
}
};
或者有效负载是否包含
{token:string}

Edit:Aight,仔细查看第一个
tokenReducer
后,我认为它只减少了
标记,而不是对象。因此正确的
初始状态应该是:

//这等于'IState'中的'token'`
常量initialState:字符串| null=null

是的,分配
state=initialState,
在语法上是正确的。

问题可能是:您根本没有返回状态。您只是覆盖它或仅返回令牌(而不是作为对象),这取决于您的负载。我假设
LOGIN
-action将返回一个令牌。因此,正确的减速器如下所示:

export const tokenReducer=(
状态:IState['token']=初始状态,
{type,payload}:AppAction,
):IState=>{
开关(类型){
案例“登录”:
返回{
……国家,
令牌:有效载荷,
};
违约:
返回状态;
}
};
或者有效负载是否包含
{token:string}

Edit:Aight,仔细查看第一个
tokenReducer
后,我认为它只减少了
标记,而不是对象。因此正确的
初始状态应该是:

//这等于'IState'中的'token'`
常量initialState:字符串| null=null

是的,分配
state=initialState,
在语法上是正确的。

有什么错误?更新了@emilebergeron有什么错误?更新了@emilebergeron这给了我
类型“null”不能分配给类型“IState”。ts(2322)
使用action.payload代替action(更新了qs)我草率地看着
tokenReducer
。请参阅我编辑的帖子。仍然给
类型'null'不能分配给类型'IState'。ts(2322)
状态:IState=initialState,````和
属性'payload'在类型{Type:“LOGIN”;payload:string;}{Type:“LOGOUT”}.ts(2339)``在
{Type,payload}上不存在:
当然,它不再是
IState
,而是接受
字符串的
令牌
。我已经更新了减速器。是的,然后你必须将
AppAction
显式转换成一个
LoginAction
,它确实包含有效载荷。这给了我
类型“null”不能分配给类型“IState”。ts(2322)
使用action.payload代替action(更新的qs)我草率地看了
tokenReducer
。请参阅我编辑的帖子。仍然给
类型'null'不能分配给类型'IState'。ts(2322)
状态:IState=initialState,````和
属性'payload'在类型{Type:“LOGIN”;payload:string;}{Type:“LOGOUT”}.ts(2339)``在
{Type,payload}上不存在:
当然,它不再是
IState
,而是接受
字符串的
令牌
。我已经更新了减速器。是的,那么您必须将
AppAction
显式地转换为一个
LoginAction
,它确实包含有效负载。
'initialState' refers to a value, but is being used as a type here. Did you mean 'typeof initialState'?ts(2749)
``
What am I doing wrong? Am I making a syntax error or is it just not allowed to define initialStates like this?