使用打字稿进行React + Redux。如何处理我想要的有效载荷类型

时间:2019-07-01 10:42:34

标签: reactjs typescript redux

我不想使用任何类型。 我该如何更改这些类型,使其在未使用的情况下都能正常工作。

e.tartget.name的类型是字符串。 因为类型不相等,所以我无法获得想要的有效载荷。

我认为该错误是为什么我不擅长打字稿技能的原因。 请帮助我...

  handleChange = (e: FormEvent<HTMLInputElement>) => {
    const { value, name }= e.currentTarget;
    this.props.UserRegisterActions.setUserData({
      key: name,
      value,
    });
  };
import { handleActions, createAction } from 'redux-actions';
import produce from 'immer';

export interface RegisterFormType {
  workerName: string;
  email: string;
  salary: number;
  join_date: string;
  birth: string;
  grade: string;
};
export type RegisterPayloadType = {
  key: RegisterFormType;
  value: any;
};
const SET_USER_DATA = 'board/SET_USER_DATA';

export const userRegisterActions = {
  setUserData: createAction(
    SET_USER_DATA,
    ({ key, value }:any) => ({
      key ,
      value
    }),
  ),
};

type SetUserData = ReturnType<typeof userRegisterActions.setUserData>;

export type RegisterState = {
  register: any; //here
};
const initialState: RegisterState = {
  register: {
    email: '',
    workerName: '',
    salary: 0,
    join_date: '',
    grade: '',
    birth: '',
  },
}
const userRegister = handleActions<RegisterState, any>(
  {
    [SET_USER_DATA]: (state, { payload: { value, key } }: SetUserData) => {
      return produce(state, draft => {
        draft.register[key] = value;
      });
    },
  },
  initialState,
);
export default userRegister;

0 个答案:

没有答案