如何键入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类型,因此添加了我自己的接口,该接口将对象标识为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 }
> {}