Fork me on GitHub
#clojurescript
<
2023-03-20
>
Rob Haisfield17:03:20

I’d like to use the LangChain TypeScript library in ClojureScript, and get useful typehints. Has anyone written or created a guide to interop? I know about JavaScript interop, but I’d like to still use the types, or a ClojureScript idiomatic equivalent like Malli

Clojuri0an17:03:22

Trying to read interop docs but having trouble. I've got a

(def foo (<p! api-auth (.-prop) (.make #js { ... } ))
that returns foo as a json array correctly. Then I need to access bar from foo, in js: " foo.bar" (. bar is returned from .make (within foo), it is not part of the original #js {} that .make pushes to the remote api to return foo). After this, I need to post a property from the api response
(.json res #js {:key (.-bar foo)})
This returns
cannot infer target type in expression (. foo -bar)

p-himik17:03:34

That's a warning, not an error. Add ^js in front of foo.

p-himik17:03:24

Also, if that <p! comes from cljs.core.async.interop then its usage is wrong.

Clojuri0an17:03:46

Ty, how to properly use <p! for a promise (need to wait for api to return foo)

p-himik17:03:42

I wouldn't use <p! if you aren't already using core.async in your UI. I would just use regular JS promise interop. But if you do use core.async already, then <p! accepts just a single argument - the promise.

Clojuri0an17:03:54

Actually I think I wrote it inaccurately on slack. What I have written is

(def ^js foo (<p! (-> api-auth (.-prop) (.make #js {...}) ....
Does this take one thread argument (-> api-auth ...) I'm not sure about what needs to be used as I don't have the best grasp of async functions in js. In this case, this is the backend and not the UI. The original functions from have JS tht use async. The code is for a nodejs express server as an api

p-himik17:03:15

That makes more sense. But only syntactically. It's still wrong from the point of view of the whole execution. In the above code, <p! needs to be in a go block. You can't have it in a def. Just use regular JS promises.

(-> (.-prop api-auth)
    (.make #js {...})
    (.then (fn [^js foo]
             (do-stuff-to (.-bar foo)))))

2
Clojuri0an17:03:52

Thanks again

👍 2