首页 > 解决方案 > 为 Redux 操作设置 Typescript 接口(类型“...”没有调用签名)

问题描述

我开始探索 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
);

还原动作

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);
};

标签: reactjstypescriptreduxreact-redux

解决方案


推荐阅读