用泛型来写redux和axios类型

140 阅读1分钟
//redux相关类型
import type { ThunkAction, ThunkDispatch } from "redux-thunk"
import store from "@/store"
import type { RootResData } from "./data"
import { updateUserType } from "@/store/actions/profile"
export type RootStore = ReturnType<typeof store.getState>

export type RootThunkAction = ThunkAction<
  void,
  RootStore,
  unknown,
  action<RootResData |string>
>
//ThunkDispatch
export type AppThunkDispatch = ThunkDispatch<
  RootSate,
  unknown,
  action<RootResData |string>
>
export type actionType =
  | "login/token"
  | "user/get"
  | "profile/getUserProfile"
  | "profile/update"
  | "profile/photo"
export type action<P> = {
  type: actionType
  payload: P
}

export type ActionCreator<P> = (payload: P) => action<P>
export type ReducerCreator<P, A = action<P>> = (state: P, action: A) => P




import { updateUserType } from "@/store/actions/profile"

//所有跟接口相关的数据的类型
interface ResponseData<D> {
  message: string
  data: D
}

//接口返回的数据类型
export type Token = {
  token: string
  refresh_token: string
}
//Profile页面的数据类型

export type User = {
  id: string
  name: string
  photo: string
  art_count: number
  follow_count: number
  fans_count: number
  like_count: number
}
//编辑时的个人信息
export type UserEditor = {
  id: string
  name: string
  photo: string
  mobile: string
  gender: string
  birthday: string
  intro?: string
}
export type ProfileType = {
  user: User
  userProfile: UserEditor
}
export type UserPhoto = {
  id?: string
  photo: string
}