Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Collection creation in JS is failing with TypeScript 3.4 #225

Open
thePanz opened this issue May 10, 2024 · 1 comment
Open

Collection creation in JS is failing with TypeScript 3.4 #225

thePanz opened this issue May 10, 2024 · 1 comment

Comments

@thePanz
Copy link

thePanz commented May 10, 2024

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)
@jasonbosco
Copy link
Member

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.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants