I've got a type-error with the js-client when call...
# community-help
h
I've got a type-error with the js-client when calling multiSearch.perform(). Is it my typing that results in the UnionSearchResponse or is there something else going on? Code
Copy code
const response = typesense.multiSearch.perform<[product_categories: ProductCategory, products: ProductSearch]>(
  {
    searches: [
      {
        collection: 'product_categories',
        q: searchValue.value,
        query_by: 'name,description',
        limit: 5,
      },
      {
        collection: 'products.search',
        q: searchValue.value,
        ...searchParams.value,
        limit: 4,
      },
    ],
  },
)
The type returns as
UnionSearchResponse<ProductCategory | ProductSearch>
instead of
MultiSearchResponse<[product_categories: ProductCategory, products: Product]>
Typesense ^2.0.3
1
f
I've just tried to write a script to replicate this but:
Copy code
type ProductCategory = {
  id: string;
  name: string;
  description: string;
};

type ProductSearch = {
  id: string;
  name: string;
  description: string;
  category_id: string;
};

const client = new Client({
  nodes: [
    {
      host: "localhost",
      port: 8108,
      protocol: "http",
    },
  ],
  apiKey: "xyz",
});

const searchParams = {
  value: {
    q: "product",
    query_by: "name,description",
    sort_by: "_text_match:desc",
    facet_by: "category_id",
    facet_query: "category_id:(1,2)",
    filter_by: "category_id:=[1,2]",
    highlight_fields: "name,description",
  },
};

const response = client.multiSearch.perform<
  [product_categories: ProductCategory, products: ProductSearch]
>({
  searches: [
    {
      collection: "product_categories",
      q: "product",
      query_by: "name,description",
      limit: 5,
    },
    {
      collection: "products.search",
      ...searchParams.value,
      limit: 4,
    },
  ],
});

const b = await response;
console.log(b.results[0]);
There's no errors here. The result is correctly being inferred. Can you post the result of
npm list typesense
?
Copy code
❯ pnpm list typesense
Legend: production dependency, optional only, dev only

vite-project@0.0.0 /home/fanis/code/test/vite-project (PRIVATE)

dependencies:
typesense 2.0.3
👀 1
Also, what version of Typesript are you using?
h
typesense@2.0.3
typescript@5.8.2
f
Even with
5.8.3
it's still the exact same for me. Are you sure you haven't used
union:true
anywhere? Can you post the full snippet with the searchValue object?
h
I created a new project, using only your script above and are still getting the error
Copy code
{
  "name": "untitled",
  "version": "0.0.0",
  "private": true,
  "type": "module",
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "preview": "vite preview"
  },
  "dependencies": {
    "typescript": "^5.8.3",
    "typesense": "^2.0.3"
  },
  "packageManager": "pnpm@10.10.0+sha512.d615db246fe70f25dcfea6d8d73dee782ce23e2245e3c4f6f888249fb568149318637dca73c2c5c8ef2a4ca0d5657fb9567188bfab47f566d1ee6ce987815c39"
}
f
Can you publish it to GitHub So I can fork it?
h
sure, a couple of secs
👍 1
f
The issue is that your type checking isn't being strict enough, which is causing TypeScript to get confused about the union types in your multi-search implementation. When you have
strict: false
in your
tsconfig.json
, TypeScript becomes too lenient and starts making assumptions about your types that aren't correct. In your case, it's incorrectly assuming the union: true case even when you haven't specified it, which is why you're getting that type error about the results property. The fix is super simple. Add
"strict": true
to your
tsconfig.json
. This will make TypeScript properly respect your type definitions and correctly handle the discriminated union types in your multi-search implementation. This is actually a good example of why strict mode is recommended. It helps catch these kinds of type inference issues that could lead to runtime errors. The more permissive
strict: false
setting might seem easier to work with, but it can hide potential type safety issues that could cause problems in your code.
h
Thank you for looking into it! I've inherited this project and are working on the typing and will absolutely work towards enabling strict typechecking.
Good to know the error is on my end
f
Super tricky to find as well. If you hadn't provided the full repo, never would have I guessed that
strict
true would be the issue here
h
Yeah, it's actually a Nuxt 3 project, and i just used their default tsconfig. Surprised they got strict disabled by default tbh.
f
strict:true
can lead to real head-scratchers in terms of typing intrinsics! They should have their reasons I'm sure
👍 1