【发布时间】:2017-08-18 04:49:08
【问题描述】:
假设我有这样的代码:
import { Action, Dispatch } from 'redux';
import { ThunkAction } from 'redux-thunk';
interface StateTree {
field: string;
}
function myFunc(action: Action | ThunkAction<void, StateTree, void>,
dispatch: Dispatch<StateTree>) {
dispatch(action); // <-- This is where the error comes from
}
...我从 TypeScript 编译器收到此错误:
ERROR in myFile.ts:x:y
TS2345: Argument of type 'Action | ThunkAction<void, StateTree, void>' is not assignable to parameter of type 'Action'.
Type 'ThunkAction<void, StateTree, void>' is not assignable to type 'Action'.
Property 'type' is missing in type 'ThunkAction<void, StateTree, void>'.
我认为问题在于 redux-thunk 类型定义文件增强了 redux Dispatch 接口的方式以及 TypeScript 无法知道要使用 Dispatch 的哪个定义。
有没有办法解决这个问题?
【问题讨论】:
标签: typescript redux redux-thunk