我在 Redux Observable 中收到以下错误。 我该如何解决这个问题?
'OperatorFunction
export interface ITodo {
id: number;
title: string;
}
interface IPayloadAction extends Action {
type: string;
payload?: any;
}
const todoFetchEpic: Epic<IPayloadAction> = (actions$) =>
actions$.pipe(
ofType(todoActions.fetch.started.type),
mergeMap((action: IPayloadAction) =>
concat(
of(todoActions.loading({ isLoading: true })),
ajax.getJSON(action.payload.url).pipe(
map((todos: ITodo[]) =>
todoActions.fetch.done({
params: action.payload.url,
result: {todos}
})
),
catchError((_) =>
of(
todoActions.fetch.failed({
params: action.payload.url,
error: { hasError: true },
})
)
)
),
of(todoActions.loading({ isLoading: false }))
)
)
);
答案 0 :(得分:0)
请检查AjaxCreationMethod
界面:
export interface AjaxCreationMethod {
// ... other methods ...
getJSON<T>(url: string, headers?: Object): Observable<T>;
}
您可以看到,对于 getJSON
,您可以为响应指定通用类型。
没有它,编译器对返回的数据的形状一无所知,并假设 unknown
。
按如下方式更改您的代码:
ajax.getJSON<ITodo[]>(action.payload.url)