ice-lab / icestore

🌲 Simple and friendly state for React
MIT License
397 stars 35 forks source link

RFC: effects 中的 dispatch 和 rootState 类型提示支持 #143

Closed hzyhbk closed 2 years ago

hzyhbk commented 2 years ago

Problem Description

当前在 effects 中 使用 dispatchrootState 时并没有完整的类型提示, 有一种方式是通过as unknown as xx 的形式来断言,但这样在每次使用的时候都需要写一遍断言,体验相当不好。

Proposed Solution

在 @ice/store 先声明两个 interface RootStateRootDisaptch

image

然后在实际项目中通过declare module '@ice/store' 的方式来扩大这两个类型

image

这样操作之后就可以得到完整的类型提示了

image image

但是这种方式的缺点就是需要自己把每个 model 都写一遍。尝试着用 models 的方式写,会报【以递归方式将自身引用为基类】的错误

image

当然每个 model 都写一遍声明,这种没啥意义的重复劳动可以在 ice 或者 rax 的 store 插件中通过工程化的方式来自动生成,做到用户无感。

hzyhbk commented 2 years ago

找到了一种对源码修改更少的方式:

  1. 新增一个 IcestoreModels 类型

image

  1. IcestoreRootStateIcestoreDispatch 泛型的默认值设置为 IcestoreModels

image

image

  1. 在项目中手动扩大类型

image

效果如下:

image

image