微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

未知不可分配给类型:Redux Observable 中的 Typescript 错误

如何解决未知不可分配给类型:Redux Observable 中的 Typescript 错误

我在 Redux Observable 中收到以下错误。 我该如何解决这个问题?

'OperatorFunction>' 不能分配给 'OperatorFunction>'。 类型 'unkNown' 不能分配给类型 'ITodo[]'.ts(2345)

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 举报,一经查实,本站将立刻删除。