algolia / algoliasearch-client-javascript

⚡️ A fully-featured and blazing-fast JavaScript API client to interact with Algolia.
https://www.algolia.com/doc/api-client/javascript/getting-started/
MIT License
1.33k stars 222 forks source link

[bug]: client-common throw types error when noUncheckedIndexedAccess is enable #1561

Closed RodrigoTomeES closed 1 month ago

RodrigoTomeES commented 1 month ago

Description

If you has enable "noUncheckedIndexedAccess": true, in your project you can't compile Next.js projects because the package produce types error.

It can be fixed checking the index.

export function shuffle<TData>(array: TData[]): TData[] {
  const shuffledArray = array;

  for (let c = array.length - 1; c > 0; c--) {
    const b = Math.floor(Math.random() * (c + 1));
    const a = array[c];

    // Added the ifs
    if (array[b]) shuffledArray[c] = array[b];
    if (a) shuffledArray[b] = a;
  }

  return shuffledArray;
}

Client

All

Version

5.8.1

Relevant log output

node_modules/@algolia/client-common/src/transporter/helpers.ts:11:5 - error TS2322: Type 'TData | undefined' is not assignable to type 'TData'.
  'TData' could be instantiated with an arbitrary type which could be unrelated to 'TData | undefined'.

11     shuffledArray[c] = array[b];
       ~~~~~~~~~~~~~~~~

node_modules/@algolia/client-common/src/transporter/helpers.ts:12:5 - error TS2322: Type 'TData | undefined' is not assignable to type 'TData'.
  'TData' could be instantiated with an arbitrary type which could be unrelated to 'TData | undefined'.

12     shuffledArray[b] = a;
       ~~~~~~~~~~~~~~~~

node_modules/@algolia/client-common/src/transporter/helpers.ts:71:5 - error TS2322: Type 'string | undefined' is not assignable to type 'string'.
  Type 'undefined' is not assignable to type 'string'.

71     serializedHeaders[header.toLowerCase()] = value;
       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Found 3 errors in the same file, starting at: node_modules/@algolia/client-common/src/transporter/helpers.ts:11
shortcuts commented 1 month ago

Hey, thanks for reporting the issue! We shouldn't provide the typescript files in order to prevent those kind of issues, next release will contain the fix :)