【发布时间】:2021-11-09 05:34:45
【问题描述】:
我有这行代码,它是一个包装函数,即使它工作正常,
const to = (promise: any) => promise.then((data: any) => data).catch((err: any) => err);
但是如果我不包含 eslint-disable-next-line,eslint 会提示意外的任何错误
ESLint: Unexpected any. Specify a different type.(@typescript-eslint/no-explicit-any)
函数包装器to()可用于包装不同的登录方法,例如此函数,
const loginWithEmail = async (email:string, password:string) => new Promise((reject) => {
const firebase = getFirebase();
firebase.login({
email,
password,
}).catch((error) => {
reject(error);
return error.message;
});
});
最终结果被放置在一个 React 组件中,在那里它将获取错误消息并将其放入要显示的钩子中。
const handleEmailLogin = async () => {
const err = await to(loginWithEmail(email, password));
setEmailLoginError(err.message);
};
我想知道是否有更好的方法来定义函数 to() 而不依赖于类型 any?
【问题讨论】:
标签: reactjs typescript function firebase-authentication react-hooks