关于RootState中使用ReturnType的问题
来源:4-11 【讨论题】React Hooks 深度分析

samLau1984
2023-02-02
阿老师好,请问下,export type RootState = ReturnType
阿老师好,请问下,export type RootState = ReturnType<typeof store.getState> 这里为什么一定要用ReturnType呢?typeof store.getState 不是已经可以直接返回了store的state的类型了吗? 为什么还需要ReturnType?
而 export type APPDispatch = typeof store.dispatch 就可以了?
写回答
1回答
-
阿莱克斯刘
2023-02-08
typeof 获得的是当前对象的类型,而ReturnType得到的是对象的返回类型。
因为store.getState是一个函数,所以typeof得到的只是这个函数类型,而我们需要的是getState函数返回值的类型,所以需要使用ReturnType。
这里又一篇文章可以研究一下,typescript关键词Parameters和ReturnType_ts parameters_只想挣钱,呜呜的博客-CSDN博客
00
相似问题