我有一个简单的函数,它接受一个函数作为参数并返回一个新函数。当调用返回的函数
时,我得到对象的类型为'未知'export function fetchCount(amount: number) {
return new Promise(resolve => setTimeout(() => resolve({ data: amount }), 500));
}
export const incrementAsync = createAsyncThunk('counter/fetchCount', async (amount: number) => {
const response = await fetchCount(amount);
return response.data; // red wave info under response
});
red wave info:const response: unknown Object is type of 'unknown'.ts(2571)
Promise<T>
是泛型类型,在您的情况下无法推断该类型,因此您需要提供泛型类型参数:
export function fetchCount(amount: number) {
return new Promise<{data: number}>(
(resolve) => setTimeout(() => resolve({ data: amount }), 500));
}