Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular NGRX/存储有效负载类型混淆_Angular_Typescript_Ngrx - Fatal编程技术网

Angular NGRX/存储有效负载类型混淆

Angular NGRX/存储有效负载类型混淆,angular,typescript,ngrx,Angular,Typescript,Ngrx,我有以下行动: export const ActionTypes = { CREATE_OH: type('[ORDERHEAD] Create Orderhead'), MODIFY_SELECTED_OH: type('[ORDERHEAD] Select Orderhead'), }; export class CreateOHAction implements Action { type = ActionTypes.CREA

我有以下行动:

export const ActionTypes = {
  CREATE_OH:                  type('[ORDERHEAD] Create Orderhead'),
  MODIFY_SELECTED_OH:    type('[ORDERHEAD] Select Orderhead'),     
};

export class CreateOHAction implements Action {
  type = ActionTypes.CREATE_OH

  constructor(public payload: OrderHead[]) { }
}

export type Actions
  =   CreateOHAction
  |   SelectOHAction;
使用以下基本减速器设置

export interface State {
  orderids: string[];
  entities: { [orderID: string]: OrderHead };
  selectedOhID: string | null;
};

// Set initial state to empty
const initialState: State = {
  orderids : [],
  entities: {},
  selectedOhID: null,

};
export function OHreducer(state = initialState, action: orderhead_imp.Actions):  State{
      switch(action.type){

        case orderhead_imp.ActionTypes.CREATE_OH: 
            let orders = action.payload;
            let newOrders = orders.filter(orderhead => !state.entities[orderhead.orderID]);

            let newOrderIds = newOrders.map(orderhead => orderhead.orderID);
            let newOrderHeadEntities = newOrders.reduce((entities: { [orderID: string]: OrderHead }, orderhead: OrderHead) => {
              return Object.assign(entities, {
                [orderhead.orderID]: orderhead
              });
            }, {});

            return {
              orderids: [ ...state.orderids, ...newOrderIds ],
              entities: Object.assign({}, state.entities, newOrderHeadEntities),
              selectedOhID: state.selectedOhID
              };

        default:
            return state;

    };
}
但是,如果我引入另一个操作,这很好:

export class SelectOHAction implements Action {
  type = ActionTypes.MODIFY_SELECTED_OH 

  constructor(public payload: string) { }
}
请注意,仅此操作的有效负载是字符串,一旦保存或尝试编译,typescript现在声明:“类型字符串上不存在筛选器| OrderHead[]”

现在,如果我进入减速器,并添加一个新案例:

case orderhead_imp.ActionTypes.MODIFY_SELECTED_OH: {
 return {
  orderids:  state.orderids,
  entities: state.entities,
  selectedOhID: action.payload
};
}
映射action.payload of时,我收到typescript错误:

抛出TS错误“字符串| OrderHead[]不可分配给类型字符串”

显然,在这两种情况下,有效负载具有不同的数据结构,我是否需要以任何其他方式更改代码,以确保每种情况都选择了正确的操作类型。有效负载

更新

因此,如果在我的操作中,我将有效负载定义为“any”而不是“string”,那么它似乎可以编译并正常工作,但是这看起来非常粗糙(而不是预期的行为)


这是Typescript>2.1和ngrx的类型util的问题

现在使用typescript 2.1及以上版本,您可以简单地将操作定义为

export const CREATE_OH: '[ORDERHEAD] Create Orderhead';
export class CreateOHAction implements Action {
   type = CREATE_OH

   constructor(public payload: OrderHead[]) { }
}

现在,无论您在何处使用
item.ActionTypes.CREATE_OH
,都将其替换为
item.CREATE_OH
。typescript 2.1的类型将按预期流动,在您的状态下,请尝试
selectedOhID:string | null
到just
selectedOhID:string
否,我首先尝试了这一点,但是如果我在任何selectedOhID的减缩器中设置type,它仍然失败,这是操作中定义的有效负载值的类型检查错误,减速器类型定义中没有错误。您使用的是什么版本的typescript?typescript 2.1+中有一个突破性的更改(可以说是一个bug修复),它会影响字符串文字量(从而影响有区别的联合)。2.1.5,这是一个已知的问题还是现在有修复程序?有关于这个问题的更新吗?我使用的是ngrx store and effects 6.0.1,我的问题与原来的帖子完全相同。我已经改变了我的行为,问题依然存在。
export const CREATE_OH: '[ORDERHEAD] Create Orderhead';
export class CreateOHAction implements Action {
   type = CREATE_OH

   constructor(public payload: OrderHead[]) { }
}