Closed timotheeduran closed 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.
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
andsearchForFacets
methods respectively.
Not in the liteClient
from algoliasearch/lite
, though, right? 😞
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
andsearchForFacets
methods respectively.Not in the
liteClient
fromalgoliasearch/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!
Hey @wKovacs64, this will be available in 5.1.0 in a few minutes :)
Fantastic, thanks @shortcuts !
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:
Clone the Reproduction Repository: The reproduction code is available in the following GitHub repository: https://github.com/timotheeduran/algolia-repro.
Install Dependencies: Ensure all required dependencies are installed by running:
Replace Algolia Credentials: Update
ALGOLIA_APPLICATION_ID
andALGOLIA_API_KEY
in thesrc/index.ts
file with your actual Algolia credentials.Run TypeScript Compilation: Execute the TypeScript compiler to check for any typing issues:
Observe the Error: You should see the following TypeScript error in the terminal:
Run the Code to Verify Runtime Behavior: To demonstrate that the code works correctly at runtime, run the code using Node.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 typeSearchResult<ResultDTO>
. However, at runtime, the code works correctly and returns the expected search results with thehits
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!