#community-help

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.

Powered by Struct AI
29
18mo
Solved
Join the chat
Mar 17, 2022 (18 months ago)
Stefan
Photo of md5-d6c265b4792dbf0a1d6ae378f39d8736
Stefan
11:20 AM
Hi, How can I import the typesense-js client types without directly importing them from the lib?
11:21
Stefan
11:21 AM
the package.json doesn't seem to include a "types": "xx" attribute.
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
01:19 PM
I can add that in and publish a new version Stefan.

I'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
Photo of md5-d6c265b4792dbf0a1d6ae378f39d8736
Stefan
01:23 PM
I am also a noob, so I don't really know what I am doing 😄 .

But 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
01:23
Stefan
01:23 PM
Ah and where I am using typesense types is for example: I have a prop in Vue and I would like to tell typescript that this prop is of type "hit" or something
01:24
Stefan
01:24 PM
Felt like the right way to approach 🤷‍♂️
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
01:24 PM
I see! Good to know
01:24
Jason
01:24 PM
Seems reasonable to me as well!
03:11
Jason
03:11 PM
Stefan I've published 1.3.0-5 of typesense-js. Could you give it a shot now?
Mar 18, 2022 (18 months ago)
Stefan
Photo of md5-d6c265b4792dbf0a1d6ae378f39d8736
Stefan
07:00 AM
I've tried it and I still can't import the type from just the "typesense". I guess the issue is that those types are not imported there.

But 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
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
01:16 PM
CC Damian who helped covert the codebase to Typescript.

Damian - any recommendations for this ^
Damian
Photo of md5-bae2dd117ba2dd0626429c37e3d31153
Damian
01:29 PM
Could you provide the exact location of the type you are trying to use? My time is a bit limited so I'll chime in when I can:

1. 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
Photo of md5-d6c265b4792dbf0a1d6ae378f39d8736
Stefan
03:03 PM
Thank you Damian for the detailed answer!

I 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
Photo of md5-bae2dd117ba2dd0626429c37e3d31153
Damian
03:32 PM
If we re-export the type from Typesense.ts, you should be able to import it from there without using the whole path. I'd give it a try but my time is limited at the moment.
Mar 21, 2022 (18 months ago)
Stefan
Photo of md5-d6c265b4792dbf0a1d6ae378f39d8736
Stefan
02:50 PM
Jason would you be okay with this? Especially w.r.t. maintenance
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
03:52 PM
Stefan Yeah, I'd be ok with that.

Btw, 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?
03:53
Jason
03:53 PM
May be one thing you could try to verify this is manually copy the definition for SearchResponseHit into Typesense.d.ts in your node modules folder and see if that helps in any way?
03:53
Jason
03:53 PM
If it does, then I can publish a new version of the library with all the types re-exported from the main Typesense.d.ts
Mar 22, 2022 (18 months ago)
Stefan
Photo of md5-d6c265b4792dbf0a1d6ae378f39d8736
Stefan
07:09 AM
Jason yes that worked! Just re-importing and exporting again and typescript ignores the lib for checking!

I 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
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
06:21 PM
Stefan Great to hear that! I'd prefer if you could do a PR with the types you need, and I can then take it from there and add the remaining ones. This way you can double-confirm that your PR works as intended for you, and I can just replicate the same pattern for other types
06:21
Jason
06:21 PM
re: which types to export, I actually assumed that all types (except the ones in ApiCall) will be used by users...
Mar 23, 2022 (18 months ago)
Stefan
Photo of md5-d6c265b4792dbf0a1d6ae378f39d8736
Stefan
07:22 AM
Jason working on it. Quick question: Is there any reason why there is not typescript eslint installed?
07:31
Stefan
07:31 AM
Also this is an interesting design decision:

I 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]
07:31
Stefan
07:31 AM
So we'd have Document.[Type] instead of all the individual types
02:38
Stefan
02:38 PM
Jason I've submitted a draft pulle request: https://github.com/typesense/typesense-js/pull/116
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
04:51 PM
> Is there any reason why there is not typescript eslint installed?
No 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?
04:53
Jason
04:53 PM
> https://typesense-community.slack.com/archives/C01P749MET0/p1648020690665759?thread_ts=1647516018.829819&cid=C01P749MET0
Do 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
Photo of md5-d6c265b4792dbf0a1d6ae378f39d8736
Stefan
07:46 AM
Jason I answered here: https://github.com/typesense/typesense-js/pull/116

It's probably easier to continue the converstation