typescript - Unknown not assignable to type:Redux Observable 中的 Typescript 错误
问题描述
我在 Redux Observable 中收到以下错误。我将如何解决这个问题?
'OperatorFunction<ITodo[], Action<{ params: { url: string; 类型的参数 }; } & { 结果:{ 待办事项:ITodo[]; }; }>>' 不能分配给 'OperatorFunction<unknown, Action<{ params: { url: string; 类型的参数。}; } & { 结果:{ 待办事项:ITodo[]; }; }>>'。类型“未知”不可分配给类型“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)
推荐阅读
- terraform - 如何从 vpc terraform 模块创建单个公共和私有路由表
- python - 根据另一个单元格中的字符串在 DataFrame 单元格的列表中插入值
- amazon-web-services - Redshift 数据库用户无权担任 IAM 角色
- c# - 以与非 API 异常不同的方式处理 API 异常。将代码移动到外部类库时出错
- amazon-web-services - 使用 Let's Encrypt 时,是否需要为 443 提供额外的 VirtualHost 条目?
- android - Android Studio 无法启动,原因是 Check Point 防病毒软件将 kotlin-stdlib.jar 移动到隔离区
- javascript - chart.js 折线图每 5 秒更新一次?
- azure - Azure - 集中管理客户端机密
- java - Java ProcessBuilder问题——目录问题
- python - 从 32 升级到 fedora 33 后,dnf 不再工作