#community-help

Typescript Support and Import Issues with Typesense

TLDR Ahmad faced trouble with Typesense support for Typescript. Jason assisted by suggesting checks and solutions. Phil suggested modifying the import statement. Ultimately, the issue was resolved with a change from Jason in version v1.1.2.

Powered by Struct AI
Dec 22, 2021 (25 months ago)
Ahmad
Photo of md5-4a6338a1d6016269c9f234fc1a133144
Ahmad
09:54 PM
Does Typesense supports typescript properly ?
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
09:55 PM
The latest version of JS/TS client library does support typescript... Are you running into any particular issues?
Ahmad
Photo of md5-4a6338a1d6016269c9f234fc1a133144
Ahmad
09:57 PM
Here is the sample code.
import Typesense from "typesense";


export class TypesenseDB {
  client;

  constructor() {
    console.log(Typesense);
    this.client = new Typesense.Client({
      nodes: [
        {
          host: "/////////",
          port: 443,
          protocol: "https",
        },
      ],
      apiKey: "////////",
      connectionTimeoutSeconds: 2,
    });
  }
}
09:58
Ahmad
09:58 PM
and I am getting the Typesense as undefined .
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
10:00 PM
That should work... could you double check that you've added typesense to package json and run yarn/npm install
Ahmad
Photo of md5-4a6338a1d6016269c9f234fc1a133144
Ahmad
10:01 PM
yes, that is 100% sure thing. is there any other dependency for typescript support? as I didn’t see a valid package as @types/typesense .
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
10:02 PM
As of v1 of the lib, it comes built-in with types, so there's no other external types needed (for types)
10:02
Jason
10:02 PM
Depending on your env, you might need to install @babel/runtime though
10:03
Jason
10:03 PM
But missing Babel dep will throw a different error usually
Ahmad
Photo of md5-4a6338a1d6016269c9f234fc1a133144
Ahmad
10:03 PM
"typesense": "^1.0.2",

version is also fine.
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
10:03 PM
Could you try clearing node_modules and running npm install again?
Ahmad
Photo of md5-4a6338a1d6016269c9f234fc1a133144
Ahmad
10:04 PM
yeah, trying it.
10:11
Ahmad
10:11 PM
same error
TypeError: Cannot read properties of undefined (reading 'Client')
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
10:12 PM
Hmm, ok to isolate the error, could you create a new barebones project with just typesense and @babel/runtime as dependencies, and a JS file with that snippet to see if the issue exists there?
Ahmad
Photo of md5-4a6338a1d6016269c9f234fc1a133144
Ahmad
10:19 PM
with JS file, it works. but with TS file, it doesn’t.
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
10:19 PM
Could you zip the minimal project with TS and share that with me?
Ahmad
Photo of md5-4a6338a1d6016269c9f234fc1a133144
Ahmad
10:25 PM
I just want the above simple class to be working.
10:25
Ahmad
10:25 PM
FYI
using ts-node ver. 9.1.1, typescript ver. 4.5.4
10:26
Ahmad
10:26 PM
here the the ts-node and typescript versions i have
Dec 23, 2021 (25 months ago)
Phil
Photo of md5-0ca37054c6c9042aa04fcfb92cc7d99c
Phil
01:26 AM
Is the import correct?

I think it should be either
import * as Typesense from 'typesense'
or
import { Client } from 'typesense'
Rohit
Photo of md5-35b1799bd10ed2978c0e2c07bdfa0aa5
Rohit
08:47 AM
import {Client as TypesenseClient} from "typesense";
import * as config from 'config';

export class SearchService {
  client: TypesenseClient;

  constructor() {
    if (!config.has('typesense')) {
      return;
    }

    this.client = new TypesenseClient({
      'nodes': [
        {
          'host': config.get('typesense.host'),
          'port': config.get('typesense.port'),
          'protocol': config.get('typesense.protocol')
        }],
      'apiKey': config.get('typesense.apiKey'),
      'numRetries': config.get('typesense.numRetries'), // A total of 4 tries (1 original try + 3 retries)
      'connectionTimeoutSeconds': config.get('typesense.connectionTimeoutSeconds'),
      'logLevel': config.get('typesense.logLevel'),
      'retryIntervalSeconds': 2,
      'timeoutSeconds': 4,
    });
  }
}
08:47
Rohit
08:47 AM
Ahmad This is working for me on same package version as yours
Dec 24, 2021 (25 months ago)
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
12:01 AM
Ahmad Turns out that import Typesense from 'typesense' way of importing Typesense "broke" recently after we removed a default export. I've pushed out a fix in v1.1.2. Could you upgrade to this version and try again?

More context here: https://github.com/typesense/typesense-js/issues/94#issuecomment-1000569392
12:01
Jason
12:01 AM
Thank you Joshua for catching this!

Typesense

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

Indexed 3015 threads (79% resolved)

Join Our Community