#community-help

Setting Array of Objects Type in TypeScript

TLDR Jameshwart asked how to set type of an array of objects in TypeScript. Jason advised them to use "object[]" and Typesense would infer the rest.

Powered by Struct AI

1

9
4mo
Solved
Join the chat
Jul 28, 2023 (4 months ago)
Jameshwart
Photo of md5-283d3397742c32cc2b6be6bda508c03e
Jameshwart
02:17 AM
Hi guys, how do I set the type of an array of objects in ts?
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
02:58 AM
You want to use object[]
Jameshwart
Photo of md5-283d3397742c32cc2b6be6bda508c03e
Jameshwart
03:03 AM
how do we set the type of the array inside it?
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
03:30 AM
Could you give me an example JSON document?
Jameshwart
Photo of md5-283d3397742c32cc2b6be6bda508c03e
Jameshwart
03:32 AM
[
  {
    "name": "simple",
    "nameAndType": "simple|product_type",
    "slug": "simple",
    "type": "product_type",
    "url": "https://google.com/?product_type=simple"
  },
  {
    "name": "featured",
    "nameAndType": "featured|product_visibility",
    "slug": "featured",
    "type": "product_visibility",
    "url": "https://google.com/?product_visibility=featured"
  },
  {
    "name": "Father's Day Gift Hampers",
    "nameAndType": "Father's Day Gift Hampers|product_cat",
    "slug": "fathers-day-gift-hampers",
    "type": "product_cat",
    "url": ""
  }
]

this is the sample data Jason
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
03:36 AM
If you just set object[] for the top level type, Typesense will infer the rest of the structure inside the nested array of objects
03:37
Jason
03:37 AM
But if you want to for eg, only index nestedField.slug, you can define it like this:

{name: 'nestedField.slug', type: 'string[]', optional: true}
Jameshwart
Photo of md5-283d3397742c32cc2b6be6bda508c03e
Jameshwart
03:39 AM
ah make sense now.
03:39
Jameshwart
03:39 AM
thank you

1

Typesense

Lightning-fast, open source search engine for everyone | Knowledge Base powered by Struct.AI

Indexed 3011 threads (79% resolved)

Join Our Community