为Redux动作设置Typescript接口(类型'...'没有呼叫签名)

时间:2020-07-01 00:53:28

标签: reactjs typescript redux react-redux

我开始探索Typescript和Redux + Typescript。我一直在跳动着许多不同的资源,坦率地说,对于我要在下面解决的问题,我有些不解。我在redux中设置了我的界面,命名了它的参数,并认为就足够了。不幸的是,当我试图公然创建类型错误时,什么都没有触发。我只是将alertActions.Parameters添加到setAlert中。

我要休息一下,稍后再探讨。虽然,我真的很好奇一个人如何正确处理这种情况,但是这肯定会给我提供更集中的研究途径。

我现在遇到的错误是:Type 'Parameters' has no call signatures.

反应组件

import * as alertActions from '../../redux/actions/alert';

type Props = {
  format: 'login' | 'register';
  setAlert: alertActions.Parameters;
  register: any;
  login: any;
  isAuthenticated: boolean;
};

const Form = ({
  format,
  setAlert,
  register,
  login,
  isAuthenticated,
}: Props) => {
  const [formData, setFormData] = useState({
    username: '',
    email: '',
    password: '',
    password2: '',
  });
...
// Type 'Parameters' has no call signatures. <--- Error
setAlert('Passwords do not match', 'danger');

export default connect(mapStateToProps, { ...alertActions, ...authActions })(
  Form
);

Redux操作

export enum AlertTypes {
  success = 'success',
  failure = 'failure',
  danger = 'danger',
  other = 'other',
}

export enum Scope {
  local = 'local',
  global = 'global',
}

export interface Parameters {
  msg: string;
  alertType: AlertTypes;
  scope: Scope;
  timeout: number;
}

export const setAlert = ({
  msg,
  alertType,
  scope = Scope.local,
  timeout = 10000,
}: Parameters) => (dispatch) => {
  const id = uuidv4();
  dispatch({
    type: SET_ALERT,
    payload: { msg, alertType, scope, id },
  });

  setTimeout(() => dispatch({ type: REMOVE_ALERT, payload: id }), timeout);
};

0 个答案:

没有答案