我正在将js代码重构为ts。
在我们的代码中,我们有类似的东西
const mapDispatchToProps = (dispatch: Dispatch<ContainerAction>) => ({
showProject: (id: number) => dispatch(showProject(id)),
updateProject: (id: number) => dispatch(updateProject(id)),
createApi: api => dispatch(createApi(api)),
});
export default connect(mapStateToProps, mapDispatchToProps)(Apis);
ContainerAction
如下所示
export type ContainerAction = (param: any) => void
此处ts给出了以下派发错误
Type 'ContainerAction' does not satisfy the constraint 'Action<any>'.
Property 'type' is missing in type 'ContainerAction' but required in type 'Action<any>
对于mapDispatchToProps
o overload matches this call.
The last overload gave the following error.
Argument of type '(dispatch: Dispatch<ContainerAction>) => { showProject: (id: number) => (dispatch: Dispatch<UpdateStateAction>) => Promise<void>; ... 4 more ...; destroyTableDefinition: (id: number) => (dispatch: Dispatch<...>) => Promise<...>; }' is not assignable to parameter of type 'MapDispatchToPropsParam<{ showProject: (id: number) => (dispatch: Dispatch<UpdateStateAction>) => Promise<void>; updateProject: (id: number) => (dispatch: Dispatch<UpdateStateAction>) => Promise<...>; createApi: (api: any) => (dispatch: Dispatch<...>) => Promise<...>; destroyApi: (id: number) => (dispatch: Dispatch<...'.
Type '(dispatch: Dispatch<ContainerAction>) => { showProject: (id: number) => (dispatch: Dispatch<UpdateStateAction>) => Promise<void>; ... 4 more ...; destroyTableDefinition: (id: number) => (dispatch: Dispatch<...>) => Promise<...>; }' is not assignable to type 'MapDispatchToPropsFactory<{ showProject: (id: number) => (dispatch: Dispatch<UpdateStateAction>) => Promise<void>; updateProject: (id: number) => (dispatch: Dispatch<UpdateStateAction>) => Promise<...>; createApi: (api: any) => (dispatch: Dispatch<...>) => Promise<...>; destroyApi: (id: number) => (dispatch: Dispatc...'.
Types of parameters 'dispatch' and 'dispatch' are incompatible.
Types of parameters 'action' and 'action' are incompatible.
Type 'T' is not assignable to type 'Action<any>'.
Type 'ContainerAction' is not assignable to type 'Action<any>'.ts(2769)
index.tsx(26, 41): Did you mean to call this expression?
index.d.ts(218, 5): The last overload is declared here.
Peek Problem (⌥F8)
No quick fixes available
我的showProject将会是这样的
export function show(id: number) {
return async (dispatch: Dispatch<UpdateStateAction>) => {
try {
const res = await fetch(`${API_DOMAIN}/api/projects/${id}`)
const json = await res.json()
if (json.success) {
dispatch({
type: SUCCESS_SHOW_PROJECT,
payload: {
project: json.project
}
})
}
有人可以帮我解决这个问题吗?