对象是类型'未知'关于承诺的打字稿

发布于 2025-02-13 20:49:04 字数 472 浏览 1 评论 0原文

我有一个简单的函数,该功能作为参数的函数并返回一个新功能。当调用返回的函数红波信息:const响应:未知对象类型为'

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

unknown.ts(2571)时,我得到的对象是“未知”类型

I have a simple function that takes a function as it's argument and returns a new function. I get Object is of type 'unknown' when calling the returned function

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 of type 'unknown'.ts(2571)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请叫√我孤独 2025-02-20 20:49:04

Promise< t>是一种通用类型,在您的情况下不能推断该类型,因此您需要提供该通用类型参数:

export function fetchCount(amount: number) {
  return new Promise<{data: number}>(
    (resolve) => setTimeout(() => resolve({ data: amount }), 500));
}

Promise<T> is a generic type and the type cannot be inferred in your case, so you'll need to provide that generic type argument:

export function fetchCount(amount: number) {
  return new Promise<{data: number}>(
    (resolve) => setTimeout(() => resolve({ data: amount }), 500));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文