typesense-js icon indicating copy to clipboard operation
typesense-js copied to clipboard

TypeScript for Typesense?

Open technoplato opened this issue 5 years ago • 41 comments
trafficstars

Hello, how do I get typescript support when importing this library? It isn't critical but would be nice.

technoplato avatar Feb 12 '20 16:02 technoplato

+1 would also love to see TS support

stephanoparaskeva avatar Jun 18 '20 17:06 stephanoparaskeva

Really wouldn't be hard to write the definitions for it if the code had proper jsDocs.

g5becks avatar Aug 02 '20 19:08 g5becks

I've personally not used Typsescript before, so I'd have to do some reading to get up to speed. Would love some help from more experienced Typescript experts!

jasonbosco avatar Sep 30 '20 22:09 jasonbosco

Related: PR #43

jasonbosco avatar Oct 13 '20 04:10 jasonbosco

Any updates on this? It looks the PR is done and ready to merge.

Nick-Mazuk avatar Jan 15 '21 19:01 Nick-Mazuk

Hey @jasonbosco,

Any update on the TypeScript typings. We are evaluating search solutions at this moment for a new product and this is a great feature to have

orlando avatar Jan 27 '21 14:01 orlando

Hey everyone, I need to update PR #43 to include types for the latest features available in Typesense. We're also working on v0.19 currently, which will have additional features. Once that's out in the coming weeks, I'll be able to focus on updating the PR.

In the meantime, I could definitely use some help on this, since I need to pick up Typescript on the fly.

jasonbosco avatar Jan 28 '21 06:01 jasonbosco

I would be completely happy to pick this up and maintain it or if you need a hand with anything :)

Ziao avatar Apr 18 '21 02:04 Ziao

@Ziao Thank you! That would be awesome!

I just started reading up on Typescript last week, but I could definitely use help with this. Would you be able to review PR #43? It's also missing the multi_search endpoint we added in v0.19. If you could help review + add types for the new endpoints, we can then merge that in.

jasonbosco avatar Apr 18 '21 03:04 jasonbosco

I had a look at that PR, while a good start, it's generated from the JS source files and thus contains a lot of any types, exposes private members (starting with _), and would be a monster keeping it synchronised with the actual source code while also providing high quality typings. As @goleary mentioned, using typescript instead of javascript would alleviate a lot of headaches, both now and in the future when things get out of sync.

Would you be okay with a PR where I convert the entire code base into Typescript (while ensuring the tests still pass, of course)? It's not as scary as it sounds, but would ensure that things stay in sync in the future, and you get amazing code completion in your editor. I don't think this project is under heavy development right now, so I think it wouldn't cause too many issues. I'd be happy picking this up short term. Is there a way to contact you directly, such as Discord or similar?

Ziao avatar Apr 18 '21 14:04 Ziao

@Ziao I missed Github's notification somehow.

Thank you for reviewing. It would be great to have a PR that converts the entire codebase to Typescript ❤️

I made some tiny changes in the ts-server-0.20.0-support branch and I'll be merging that to master soon. Could you branch off of that for this PR?

jasonbosco avatar Apr 20 '21 18:04 jasonbosco

Oh also, here’s our Slack channel: https://join.slack.com/t/typesense-community/shared_invite/zt-mx4nbsbn-AuOL89O7iBtvkz136egSJg

jasonbosco avatar Apr 20 '21 21:04 jasonbosco

Having the lib in TS would be amazing - so far the slowest part of working with Typesense is the time taken to keep checking the docs to find out what methods I need to call on the client (plus the time debugging the errors caused by me assuming an incorrect return type etc).

In the meantime, I knocked together some type defs myself to make my own work more productive. Might be helpful for others until official TS support lands:

https://gist.github.com/michaelbromley/fd1e6137485200654cb7104439e6a699

michaelbromley avatar Apr 21 '21 18:04 michaelbromley

Thank you for that @michaelbromley!

CC: @Ziao

jasonbosco avatar Apr 21 '21 19:04 jasonbosco

up

WatanabeThiago avatar May 24 '21 12:05 WatanabeThiago

I just wanna shout my thanks to @Ziao for your work :) I'm happily using typesense-js off your branch and it's working great. I really appreciate it!

dharijanto avatar Jul 24 '21 04:07 dharijanto

Any news on this? We urgently need this to use Typesense in our Firestore-Projects.

phihochzwei avatar Sep 29 '21 11:09 phihochzwei

@phihochzwei and everyone else,

I completed the TS conversion a while ago and updated the tests but never got around to getting the PR rebased and merged in. I'm very sorry to say that the work pressure is not allowing me to spend too much time on this at the moment, but essentially once any conflicts are solved and the work is reviewed, I'm cool having the PR merged in.

Here's the PR, btw: https://github.com/typesense/typesense-js/pull/62

Ziao avatar Sep 29 '21 11:09 Ziao

@Ziao - I really appreciate the amount of effort you've put into shaping this PR! 🙏

@dcantu476 and I have been working on getting your PR up to date and adding in some remaining types as part of #71.

jasonbosco avatar Oct 06 '21 16:10 jasonbosco

Hey everyone,

Thanks to @Ziao and @dcantu476, we now have TypeScript types in ~v1.0.0-1~ v1.0.0-3 .

There are no other breaking changes to the external API other than the switch to TypeScript. It felt like a big enough change to warrant a major version increment.

Would love to get your feedback!

jasonbosco avatar Oct 07 '21 23:10 jasonbosco

@jasonbosco @dcantu476 Woohoo! Nice one! I'll try to give it a spin this weekend and go through the changes one last time. Looks good though!

Ziao avatar Oct 07 '21 23:10 Ziao

Released a new version v1.0.0-2 with the feedback in #73.

jasonbosco avatar Oct 09 '21 19:10 jasonbosco

I gave it a try and ran into this error:

node_modules/typesense/lib/Typesense/Configuration.d.ts:31:16 - error TS2503: Cannot find namespace 'logger'.

31 logLevel?: logger.LogLevel;

At the top of Configuration.ts, should it be: import * as logger from 'loglevel'

instead of right now: import logger from 'loglevel'

The default export of loglevel doesn't seem to be the right thing.

p-young avatar Oct 10 '21 21:10 p-young

@p-young I believe you are correct, but I'll have to test it more to be sure. I also think the correct type is logger.LogLevelDesc.

In the future, it might be better to implement some abstraction around the logger so that the client doesn't have to worry about using types directly from the logger library. It'd also put it in a good place where if you need to swap the logger out for something else in the future.

dcantu476 avatar Oct 11 '21 00:10 dcantu476

Released a new version with improvements: https://github.com/typesense/typesense-js/releases/tag/v1.0.0-3

@dcantu476 The only other pending thing is: https://github.com/typesense/typesense-js/issues/32#issuecomment-939556089

jasonbosco avatar Oct 20 '21 23:10 jasonbosco

@jasonbosco I'll do a small PR for the loglevel typings and redo the import. I do want to mention though, I never was able to reproduce issues with the import for logger in general.

dcantu476 avatar Oct 21 '21 02:10 dcantu476

Thanks @dcantu476. I've released this in v1.0.0-4.

jasonbosco avatar Oct 21 '21 22:10 jasonbosco

Hi, we are currently using the Typescript version of Typesense, and i found few errors:

  • The field geo_distance_meters is missing in the SearchResponseHit interface (example in the documentation)
  • The ImportResponseFail interface contains:
    • document: DocumentSchema;
    • but it should be document: DocumentSchema[];

Thanks for this TS version anyway, feel amazing to work with it 🎉

Wiirlak avatar Nov 18 '21 16:11 Wiirlak

I'll try to debug this one: https://github.com/typesense/typesense-js/issues/32#issuecomment-973020672

akash-joshi avatar Nov 30 '21 03:11 akash-joshi

Hey, I'm currently swapping over from Algolia, and something that would be a great nice to have would be to add Generics. I do see the Issue has been opened already here https://github.com/typesense/typesense-js/issues/152

Current: const searchResults = await this.collections(collectionName).documents().search(options) searchResults return type: SearchResponse<{}>

For me to get the hits with the correct types, I need to do something like searchResults.hits?.map((hit) => hit.document) as Type[]

Suggestion: const searchResults = await this.collections(collectionName).documents().search<Type>(options) return type: SearchResponse<Type>
typeof searchResults[0].document === Type

leeran7 avatar Apr 04 '23 22:04 leeran7