store里的state如何定义ts类型?
// src/models/user.ts export const delay = (time) => new Promise((resolve) => setTimeout(() => resolve(), time));
export default { // 定义 model 的初始 state state: { name: '', id: '', },
// 定义改变该模型状态的纯函数 reducers: { update(prevState, payload) { return { ...prevState, ...payload, }; }, },
// 定义处理该模型副作用的函数 effects: (dispatch) => ({ async getUserInfo() { await delay(1000); dispatch.user.update({ name: 'taobao', id: '123', }); }, }), }; 在类似的状态管理中如何指定 state的ts类型,该如何写,比如定义了 export type CurrentUser = { id: string; account: string; nickName: string; name: string; avatar?: any; birthday: string; sex: number; email: string; phone: string; tel: string; adminType: number; lastLoginIp: string; lastLoginTime: string; lastLoginAddress: string; lastLoginBrowser: string; lastLoginOs: string; loginEmpInfo: LoginEmpInfo; apps: App[]; roles: any[]; permissions: any[] menus: menus dataScopes: any[]; tenants?: any; password?: any; enabled: boolean; accountNonExpired: boolean; accountNonLocked: boolean; credentialsNonExpired: boolean; username: string; authorities: any[]; [key: string]: any }; 该如何定义给state,求大佬解答?
推荐使用 Recoil
没问题的,请参考这个代码 ice-test
@strongyc
import { ModelConfig } from '@ice/store';
interface State {
a: string;
}
const model: ModelConfig<State> = {
state: {
a: '1',
},
};
暂时可以先通过这样的方式编写。另外,ModelConfig 这个类型将会从 ice 导出,到时候会发个版本的。
@strongyc
import { ModelConfig } from '@ice/store'; interface State { a: string; } const model: ModelConfig<State> = { state: { a: '1', }, };暂时可以先通过这样的方式编写。另外,
ModelConfig这个类型将会从ice导出,到时候会发个版本的。
使用了ModelConfig , dispatch不会提示自定义的dispatch,只会提示setState这一个方法

安装 ice.js 最新版本,可以使用以下的写法获得类型提示: https://github.com/alibaba/ice/blob/3d83bba4c48045e2554b9884c5a60851f0a5b523/examples/basic-store/src/models/user.ts#L5