我如何将ngrx actionCreator作为函数参数传递?

问题描述 投票:0回答:1

如何键入action函数的loader参数,以使其不使用[c0],而这不是ngrx导出的?

我希望像当前​​示例一样确保类型安全。

TypedAction

我可以按如下方式有效地分解TypedAction,但它丢失了指示正在返回Action的指示。有没有更好的方法?

const loadItemsAction = createAction(
  'load items',
  props<{ items: Item[] }>()
);

function loader(action: ActionCreator<
  string,
  (props: {
    items: Item[];
  }) => TypedAction<string> & { items: Item[] }   // TypedAction is not exported from ngrx!
>){
  return action({ items: [{name: 'item1'}, { name: 'item2'}]});
}
ngrx-store typescript-generics
1个回答
0
投票

我无法使用现有的ngrx类型,因此添加了我自己的接口,该接口将对象标识为ActionCreator,并允许轻松指定这些对象的类型:

function loader(action: ActionCreator<
  string,
  (props: {
    items: Item[];
  }) => { readonly type: string, items: Item[] }
>){
  return action({ items: [{name: 'item1'}, { name: 'item2'}]});
}

在原始示例中使用:

export interface ActionCreatorWithProps<TPropArgs extends object = {}>
  extends ActionCreator<
    string,
    (props: TPropArgs) => TPropArgs & { readonly type: string }
  > {}
© www.soinside.com 2019 - 2024. All rights reserved.