我严格遵守ngxs / labs immer文档,以适应我的代码并发现这种奇怪的行为。
为什么setState
在以下位置询问所有状态属性:
export class DadesStateModel {
apartats: IApartat[];
authors: IAuthor[];
}
(...)
@Action(SetArticles)
@ImmutableContext()
setArticles({setState}: StateContext<DadesStateModel>, {apartat}: SetArticles) {
setState((state: DadesStateModel) => ({
state.apartats[state.apartats.findIndex(ap=>ap.Id===apartat.Id)] = apartat;
return state;
}))
}
,并显示以下错误消息:
Argument of type '(state: DadesStateModel) => { state: DadesStateModel; (Missing): IApartat; return: DadesStateModel; }' is not assignable to parameter of type 'DadesStateModel | StateOperator<DadesStateModel>'.
Type '(state: DadesStateModel) => { state: DadesStateModel; (Missing): IApartat; return: DadesStateModel; }' is not assignable to type 'StateOperator<DadesStateModel>'.
Type '{ state: DadesStateModel; (Missing): IApartat; return: DadesStateModel; }' is missing the following properties from type 'DadesStateModel': authors.
答案 0 :(得分:0)
即使我的回答来得有点晚:由于语法错误,您仍会看到此错误。您将{}
括在({})
中,并用方括号setState((state: DadesStateModel) => { // Instead of "({"
state.apartats[state.apartats.findIndex(ap=>ap.Id===apartat.Id)] = apartat;
return state;
}); // Instead of "}));"
括起来,因此假定您要成为lambda主体的内容是lambda的返回值。只需卸下随附的支架即可。
^