如何解决未知不可分配给类型:Redux Observable 中的 Typescript 错误
我在 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 }))
)
)
);
解决方法
请检查AjaxCreationMethod
界面:
export interface AjaxCreationMethod {
// ... other methods ...
getJSON<T>(url: string,headers?: Object): Observable<T>;
}
您可以看到,对于 getJSON
,您可以为响应指定通用类型。
没有它,编译器对返回的数据的形状一无所知,并假设 unknown
。
按如下方式更改您的代码:
ajax.getJSON<ITodo[]>(action.payload.url)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。