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

TypeScript Typing Issue with response.results[0].hits in v5 #1536

Closed timotheeduran closed 3 months ago

timotheeduran commented 3 months ago

Summary: The provided reproduction code successfully queries Algolia and returns search results, but TypeScript raises an error during compilation: "Property 'hits' does not exist on type 'SearchResult<ResultDTO>'. Property 'hits' does not exist on type 'SearchForFacetValuesResponse'.ts(2339)". Despite this, the code works as expected at runtime, indicating that this is likely a typing issue within the Algolia client.

Steps to Reproduce:

  1. Clone the Reproduction Repository: The reproduction code is available in the following GitHub repository: https://github.com/timotheeduran/algolia-repro.

    git clone https://github.com/timotheeduran/algolia-repro.git
    cd algolia-repro
  2. Install Dependencies: Ensure all required dependencies are installed by running:

    npm install
  3. Replace Algolia Credentials: Update ALGOLIA_APPLICATION_ID and ALGOLIA_API_KEY in the src/index.ts file with your actual Algolia credentials.

  4. Run TypeScript Compilation: Execute the TypeScript compiler to check for any typing issues:

    npx tsc
  5. Observe the Error: You should see the following TypeScript error in the terminal:

    src/index.ts:24:32 - error TS2339: Property 'hits' does not exist on type 'SearchResult<ResultDTO>'.
     Property 'hits' does not exist on type 'SearchForFacetValuesResponse'.
  6. Run the Code to Verify Runtime Behavior: To demonstrate that the code works correctly at runtime, run the code using Node.js:

    node dist/index.js

    Ensure that you are using a real Algolia index and a valid query that is supposed to return hits. When you run the code, you will see that the hits property is correctly returned and accessible, despite the TypeScript error during compilation.

Expected Behavior: The code compiles without any TypeScript errors.

Actual Behavior: TypeScript raises an error indicating that the hits property does not exist on the type SearchResult<ResultDTO>. However, at runtime, the code works correctly and returns the expected search results with the hits property accessible and populated.

Conclusion: The issue appears to be related to the TypeScript types defined in the Algolia client. The hits property is accessible and correctly returned at runtime, but the TypeScript typings do not reflect this. This discrepancy suggests that the typings in the Algolia client need to be reviewed and potentially updated to align with the actual runtime behavior.

Please let me know if you need any further information or assistance!

shortcuts commented 3 months ago

Hey @timotheeduran thanks a lot for testing the v5 version already and for the thorough issue description

Thanks to your reproduction steps, I managed to find the issue and also tried on v4, which also has this issue. I guess TypeScript is not able to assert if the Nth element of the array is actually of type Search or Facets (see union https://github.com/algolia/algoliasearch-client-javascript/blob/main/packages/client-search/model/searchResult.ts#L6), due to the lack of type guards.

We have two helpers that could help have a proper typing, if you know you won't mix hits and facets result, you can use the searchForHits and searchForFacets methods respectively.

wKovacs64 commented 3 months ago

We have two helpers that could help have a proper typing, if you know you won't mix hits and facets result, you can use the searchForHits and searchForFacets methods respectively.

Not in the liteClient from algoliasearch/lite, though, right? 😞

shortcuts commented 3 months ago

We have two helpers that could help have a proper typing, if you know you won't mix hits and facets result, you can use the searchForHits and searchForFacets methods respectively.

Not in the liteClient from algoliasearch/lite, though, right? 😞

Oh great point, I think it wouldn't increase the bundle size by a lot, I can give it a shot on Monday!

shortcuts commented 3 months ago

Hey @wKovacs64, this will be available in 5.1.0 in a few minutes :)

wKovacs64 commented 3 months ago

Fantastic, thanks @shortcuts !