ice icon indicating copy to clipboard operation
ice copied to clipboard

store里的state如何定义ts类型?

Open strongyc opened this issue 3 years ago • 5 comments

// 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,求大佬解答?

strongyc avatar May 06 '22 08:05 strongyc

推荐使用 Recoil

usercao avatar May 08 '22 16:05 usercao

推荐使用 Recoil

REcoil好像不支持ts呀

strongyc avatar May 09 '22 03:05 strongyc

没问题的,请参考这个代码 ice-test

usercao avatar May 09 '22 08:05 usercao

@strongyc

import { ModelConfig } from '@ice/store';

interface State {
  a: string;
}

const model: ModelConfig<State> = {
  state: {
    a: '1',
  },
};

暂时可以先通过这样的方式编写。另外,ModelConfig 这个类型将会从 ice 导出,到时候会发个版本的。

luhc228 avatar May 10 '22 02:05 luhc228

@strongyc

import { ModelConfig } from '@ice/store';

interface State {
  a: string;
}

const model: ModelConfig<State> = {
  state: {
    a: '1',
  },
};

暂时可以先通过这样的方式编写。另外,ModelConfig 这个类型将会从 ice 导出,到时候会发个版本的。

使用了ModelConfig , dispatch不会提示自定义的dispatch,只会提示setState这一个方法 截屏2022-07-10 下午11 00 34

y-arookie avatar Jul 10 '22 15:07 y-arookie

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

luhc228 avatar Oct 17 '22 02:10 luhc228