Hi, How can I import the typesense-js client types...
# community-help
s
Hi, How can I import the typesense-js client types without directly importing them from the lib?
the package.json doesn't seem to include a "types": "xx" attribute.
j
I can add that in and publish a new version @Stefan Hesse. 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?
s
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
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
Felt like the right way to approach 🤷‍♂️
j
I see! Good to know
Seems reasonable to me as well!
@Stefan Hesse I've published
1.3.0-5
of typesense-js. Could you give it a shot now?
s
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.
j
CC @Damian C who helped covert the codebase to Typescript. Damian - any recommendations for this ^
d
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 Bosco 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!
s
Thank you @Damian C for the detailed answer! I am trying to import the following type:
Copy code
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?
d
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.
s
@Jason Bosco would you be okay with this? Especially w.r.t. maintenance
j
@Stefan Hesse 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?
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?
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
s
@Jason Bosco 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
j
@Stefan Hesse 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
re: which types to export, I actually assumed that all types (except the ones in ApiCall) will be used by users...
s
@Jason Bosco working on it. Quick question: Is there any reason why there is not typescript eslint installed?
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:
Copy code
import * as Errors from './Typesense/Errors'
The difference is that as a user I have to use the later as follows:
Copy code
Errors.[TYPE]
So we'd have Document.[Type] instead of all the individual types
@Jason Bosco I've submitted a draft pulle request: https://github.com/typesense/typesense-js/pull/116
j
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?
https://typesense-community.slack.com/archives/C01P749MET0/p1648020690665759?thread_ts=1647516018.829819&amp;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?
s
@Jason Bosco I answered here: https://github.com/typesense/typesense-js/pull/116 It's probably easier to continue the converstation