Typesense-JS Client Types Importation Problem.
TLDR Stefan had problems importing types from typesense-JS, causing compiler slowdowns. Jason and Damian provided guidance and agreed to export necessary types from Typesense.ts, improving import speed and reducing compiler checks.
Mar 17, 2022 (18 months ago)
Stefan
11:20 AMStefan
11:21 AMJason
01:19 PMI'm fairly new to Typescript, so apologies for a noob question - may I know the use case where you'd only want to import types from typesense-js and not the package itself?
Stefan
01:23 PMBut the issue right now is: If I try to add a typescript type within my code it imports it from the lib directory, the problem with that is, is that the compiler checks the entire typescript lib every time which makes it super slow
Stefan
01:23 PMStefan
01:24 PMJason
01:24 PMJason
01:24 PMJason
03:11 PM1.3.0-5
of typesense-js. Could you give it a shot now?Mar 18, 2022 (18 months ago)
Stefan
07:00 AMBut I honestly don't want to give a recommendation what the appropriate way is nor do I know if what I am doing is okay.
Jason
01:16 PMDamian - any recommendations for this ^
Damian
01:29 PM1. If you want to use a type, it must be exported, otherwise it's referred to as an internal library type and isn't intended to be used by consumers of the library (I'll let Jason decide what should and should not be exported / exposed ). Remember that the types you expose, you now have to maintain and provide some kind of legacy support as the library changes over time.
2. If the type itself isn't exported, just add an export in front of it. This won't fix the issue completely, but it will allow you to import the type as long as its included with another type that is exported with the package.
3. If you want to be able to simply import from
typesense
, then you need to add a line to Typesense.ts re-exporting that type like we do for other types in the library. Think of this file similar the way you would a public api end point. It's the only way to access the library. As always, chime in if you think something I said is off, but that should set us on a general path forward. I'll check back when I can. Thanks for reaching out!
Stefan
03:03 PMI am trying to import the following type:
import { SearchResponseHit } from 'typesense/lib/Typesense/Documents'
I wouldn't care about the path, if my typescript compiler wouldn't typecheck all of typesense all the time. I think it's not a configuration issue on my site, but everything I import get's typechecked ( unless it's exported properly (that is my understanding) assuming this because all the vue types are not constantly typchecked))
Would import the types at the "root"-type file fix my issue?
Damian
03:32 PMMar 21, 2022 (18 months ago)
Stefan
02:50 PMJason
03:52 PMBtw, I already see
SearchResponseHit
has an export
before its definition in Documents.ts: export interface SearchResponseHit<T extends DocumentSchema> { ...
And I also see it being available in the types file in the build here: https://github.com/typesense/typesense-js/blob/d4094648b9c0b88fb8d6f62844d4bbd81bbdd94d/lib/Typesense/Documents.d.ts#L62
So re-exporting it from the main Typesense file would only allow you to change the path like this:
import { SearchResponseHit } from 'typesense'
.I'm not sure if that alone will be sufficient to resolve your issue?
Jason
03:53 PMSearchResponseHit
into Typesense.d.ts in your node modules folder and see if that helps in any way?Jason
03:53 PMMar 22, 2022 (18 months ago)
Stefan
07:09 AMI could make a PR but I think you know better which are internal types that you wouldn't want to expose vs those that would be breaking changes anyway
Jason
06:21 PMJason
06:21 PMMar 23, 2022 (18 months ago)
Stefan
07:22 AMStefan
07:31 AMI could just re-import all the types in typescript.ts like this:
import {DocumentSchema, x, y, z..}
Then the user can import them directly
Or I could do it like the error types:
import * as Errors from './Typesense/Errors'
The difference is that as a user I have to use the later as follows:
Errors.[TYPE]
Stefan
07:31 AMStefan
02:38 PMJason
04:51 PMNo particular reason. I thought eslint already took care of linting TS as well, until WebStorm started complaining about not being able to parse
import type
. That's when I learnt eslint needs a TS plugin separately. I was trying to set this up myself earlier this week, and paused it when it said something about a useless constructor, though it's valid Typescript to define class variables via a constructor. Paused the exploration there...Thank you for taking care of this as well. I wonder why even unchanged lines are showing up as changed in Github PR diff though. Any ideas?
Jason
04:53 PMDo you know if it's a common pattern to put all Types in a namespace like I did for Errors? If so, that feels cleaner to me, so we don't clutter the top level namespace?
Mar 24, 2022 (18 months ago)
Stefan
07:46 AMIt's probably easier to continue the converstation
Typesense
Indexed 2764 threads (79% resolved)
Similar Threads
Typescript Support and Import Issues with Typesense
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`.
Troubleshooting TypeScript Error with Typesense
GM experienced an error with Typesense in TypeScript, requiring help to correct the issues. Jason helped propose solutions and adjustments to the code. Ultimately, they were able to resolve the errors and successfully implement a search function.


Solving An Issue with Typesense-js in Sveltekit Production
Francois reported an issue where a static value in typesense-js was undefined in Sveltekit's production. Jason helped debug the issue, suspecting a build problem or a circular dependency. Francois's solution eliminated this error and was tested, approved, and finally merged by Jason.



Troubleshooting Indexing Duration in Typesense Import
Alan asked about lengthy indexing times for importing documents to Typesense. Jason suggested various potential causes, including network connectivity and system resources. They later identified the problem to be an error in Alan's code.


Enhancing Typesense-JS For Improved Cache Management
Jordan requested a way to clear the cache in 'typesense-js' when a new document is added. Jason recommended a work-around and supported Jordan's idea to add a 'clearCache' method. Jordan then submitted a PR for this implementation.
