javascript Object.fromEntries出现TypeScript错误,来自提取的数据

mwecs4sa  于 2022-12-25  发布在  Java
关注(0)|答案(1)|浏览(164)

请求= {名称:url,name:url,...}我已尝试获取每个URL并返回{ name:数据,名称:数据,...}
代码

async function fetchMovies(url: string) {
  const data = await fetch(baseURL + url).then((r) => r.json());
  return data.results;
}

export const getMovies = createAsyncThunk("movies/getMovies", async () => {
  const responses = Object.entries(requests).map(async ([key, value]) => {
    try {
      const response = await fetchMovies(value);
      return [key, response];
    } catch (e: unknown) {
      if (e instanceof Error) {
        console.log(e.message);
      }
    }
  });

  const results = await Promise.all(responses);
  console.log(results); // [[string, SomeType[]], [string, SomeType[]], ...]
  return Object.fromEntries(results); // error
});

控制台日志(结果)

Array (8)
 0 ["netflixOriginals", Array] (2)
 1 ["trending", Array] (2)
 2 ["topRated", Array] (2)
 3 ["action", Array] (2)
 4 ["comedy", Array] (2)
 5 ["horror", Array] (2)
 6 ["romance", Array] (2)
 7 Array (2)
   0 "documentary"
   1 [Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, …] (20)

错误

TS2769: No overload matches this call.
  Overload 1 of 2, '(entries: Iterable<readonly [PropertyKey, any]>): { [k: string]: any; }', gave the following error.
    Argument of type '(any[] | undefined)[]' is not assignable to parameter of type 'Iterable<readonly [PropertyKey, any]>'.
      The types returned by '[Symbol.iterator]().next(...)' are incompatible between these types.
        Type 'IteratorResult<any[] | undefined, any>' is not assignable to type 'IteratorResult<readonly [PropertyKey, any], any>'.
          Type 'IteratorYieldResult<any[] | undefined>' is not assignable to type 'IteratorResult<readonly [PropertyKey, any], any>'.
            Type 'IteratorYieldResult<any[] | undefined>' is not assignable to type 'IteratorYieldResult<readonly [PropertyKey, any]>'.
              Type 'any[] | undefined' is not assignable to type 'readonly [PropertyKey, any]'.
                Type 'undefined' is not assignable to type 'readonly [PropertyKey, any]'.
  Overload 2 of 2, '(entries: Iterable<readonly any[]>): any', gave the following error.
    Argument of type '(any[] | undefined)[]' is not assignable to parameter of type 'Iterable<readonly any[]>'.
      The types returned by '[Symbol.iterator]().next(...)' are incompatible between these types.
        Type 'IteratorResult<any[] | undefined, any>' is not assignable to type 'IteratorResult<readonly any[], any>'.
          Type 'IteratorYieldResult<any[] | undefined>' is not assignable to type 'IteratorResult<readonly any[], any>'.
            Type 'IteratorYieldResult<any[] | undefined>' is not assignable to type 'IteratorYieldResult<readonly any[]>'.
              Type 'any[] | undefined' is not assignable to type 'readonly any[]'.
                Type 'undefined' is not assignable to type 'readonly any[]'.
    22 |   const results = await Promise.all(responses);
    23 |   console.log(results);
  > 24 |   return Object.fromEntries(results);
       |                             ^^^^^^^
    25 | });
    26 |

我找到的唯一解决方案是结果:任意[]。我不是很擅长TS,我已经尝试了我知道的一切。抱歉问了这个愚蠢的问题

6kkfgxo0

6kkfgxo01#

由于您没有为responses对象指定type,它将根据其操作返回的结果自动进行检测。
就你而言,

  • 您在try块中返回了任意[]的数组
  • catch块中未返回任何内容,因此也可能未定义

因此,响应类型应为(any[] | undefined)[]
对此做一些处理,(比如也在catch中返回[key,value]),那么类型将是any[][]。Object.fromEntries()将接受此类型。

相关问题