typesense-website icon indicating copy to clipboard operation
typesense-website copied to clipboard

Collection creation in JS is failing with TypeScript 3.4

Open thePanz opened this issue 1 year ago • 1 comments

Description

The JS example here: https://typesense.org/docs/26.0/api/collections.html#with-pre-defined-schema Throws an error with used with TypeScript v3.4.

The error is similar to:

Argument of type '{ name: string; fields: { name: string; type: string; facet: boolean; }[]; }' is not assignable to parameter of type 'CollectionCreateSchema'.
  Types of property 'fields' are incompatible.
    Type '{ name: string; type: string; facet: boolean; }[]' is not assignable to type 'CollectionFieldSchema[]'.
      Type '{ name: string; type: string; facet: boolean; }' is not assignable to type 'CollectionFieldSchema'.
        Types of property 'type' are incompatible.
          Type 'string' is not assignable to type 'FieldType'.

It boils down to: https://stackoverflow.com/a/37978675

How to solve it

Please add a note about that issue, or provide the following as an example:

let schema = {
  'name': 'companies',
  'fields': [
    {
      'name': 'company_name',
      'type': 'string' as const,
      'facet': false
    },
    {
      'name': 'num_employees',
      'type': 'int32' as const,
      'facet': false
    },
    {
      'name': 'country',
      'type': 'string' as const,
      'facet': true
    }
  ],
  'default_sorting_field': 'num_employees'
}

client.collections().create(schema)

thePanz avatar May 10 '24 12:05 thePanz

Thank you for documenting this.

The examples in the docs are for Javascript.

That said, is there a way to fix this issue in the typesense TS library, so users don't have to explicitly use the as const? I'm not a Typescript expert, so I'd appreciate a PR for this.

jasonbosco avatar May 14 '24 16:05 jasonbosco

Since this issue hasn't seen traction for a long time, I'm going to close this. Also, as a side note, TypeScript 3.4 is now 6 years old, and we officially support 5.0 onwards. Although, any feedback on older TypeScript versions is appreaciated!

tharropoulos avatar Mar 11 '25 09:03 tharropoulos