我正在将一个React项目迁移到TypeScript,但我正在努力键入这个函数,随后是useEffect
。我知道registerListener
必须返回Promise
,但我如何访问useEffect
中该承诺的值?
const registerListener = async () => {
const listener = await someAsyncAction();
return { listener };
};
React.useEffect(() => {
const { listener } = registerListener();
return () => {
removeListener(listener);
};
}, []);
您是否尝试在registerListener上声明返回类型?
const registerListener = async (): Promise<{ listener: someAsyncActionReturnType }> => {
const listener = await someAsyncAction();
return { listener };
};