reactjs - 为 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);
};
解决方案
推荐阅读
- java - 如何以毫秒为单位获取 UTC 日期?
- python - 将 netcdf 重新投影为正弦投影
- asp.net-core - 带有角度通用的 ASP.Net Core spa 预渲染在本地工作,但不在服务器上
- amazon-web-services - 使用 aws-sdk2 时如何在启动请求后保持会话活动
- graph - yEd 中来自 neo4j 的数据未正确加载
- ios - iOS bot上的电报错误发送多次相同的消息
- python - Panda DataFrames 中有序分类数据的最小值
- php - Google 在 PHP 应用引擎中未找到 Robots.txt
- c++ - 将没有默认 ctor 的对象插入到 std::map
- excel - Excel中基于两个值的条件格式