TypeScript:类型“ActionTypes”不满足约束“Action<any>”
Posted
技术标签:
【中文标题】TypeScript:类型“ActionTypes”不满足约束“Action<any>”【英文标题】:TypeScript: Type 'ActionTypes' does not satisfy the constraint 'Action<any>' 【发布时间】:2020-10-25 18:13:33 【问题描述】:我正在使用 React 和 Redux 编写一个 TypeScript 应用程序,但遇到了问题。
我需要第二个默认选项。
代码如下:
import ThunkAction from 'redux-thunk'
type AppThunk<ActionTypes, ReturnType = void> = ThunkAction<
ReturnType,
AppStateType,
unknown,
ActionTypes
>
type ActionTypes = followUnfollowActionType | setUsersActionType | setTotalCountActionType |
setCurrentPageActionType | toggleIsFetchingActionType | toggleFollowingInProgressActionType
type ThunkType = AppThunk<ActionTypes>
错误:
Type 'ActionTypes' does not satisfy the constraint 'Action<any>'. TS2344
6 | AppStateType,
7 | unknown,
> 8 | ActionTypes
| ^
9 | >
这里有什么问题?
【问题讨论】:
【参考方案1】:我已经找到了解决办法:
import AppStateType from '../redux/store'
import ThunkAction from 'redux-thunk'
import Action from 'redux'
export type AppThunk<ActionTypes extends Action, ReturnType = void> = ThunkAction<
ReturnType,
AppStateType,
unknown,
ActionTypes
>
【讨论】:
以上是关于TypeScript:类型“ActionTypes”不满足约束“Action<any>”的主要内容,如果未能解决你的问题,请参考以下文章