Henrik Sjödahl
05/07/2025, 8:05 AMconst 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.3Fanis Tharropoulos
05/07/2025, 8:13 AMtype 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
?
❯ 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
Fanis Tharropoulos
05/07/2025, 8:13 AMHenrik Sjödahl
05/07/2025, 8:18 AMtypesense@2.0.3
typescript@5.8.2
Fanis Tharropoulos
05/07/2025, 8:36 AM5.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?Henrik Sjödahl
05/07/2025, 8:42 AM{
"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"
}
Fanis Tharropoulos
05/07/2025, 8:44 AMHenrik Sjödahl
05/07/2025, 8:44 AMHenrik Sjödahl
05/07/2025, 8:47 AMFanis Tharropoulos
05/07/2025, 9:31 AMstrict: 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.Henrik Sjödahl
05/07/2025, 9:35 AMHenrik Sjödahl
05/07/2025, 9:35 AMFanis Tharropoulos
05/07/2025, 9:36 AMstrict
true would be the issue hereHenrik Sjödahl
05/07/2025, 9:38 AMFanis Tharropoulos
05/07/2025, 9:40 AMstrict:true
can lead to real head-scratchers in terms of typing intrinsics! They should have their reasons I'm sure