How do you type hint in CLJS

E.g. you’re writing a ClojureScript function. Do you prefer ^number or ^js/Number hint on it? And why.

What purpose should these hints serve?

^number is recognized by some areas in the analyzer while ^js/Number is not. In most cases neither has any effect on the generated code or runtime performance. I think ^number only really affects some generated warnings, not actually the generated code.

1 Like

e.g. ^boolean does for sure. see
https://blog.fikesfarm.com/posts/2015-12-04-boolean-type-hints-in-clojurescript.html

Also ^IFn and ^js/Function hinted variables will be called differently (shot myself in the leg recently)

Yes, hints sometimes make a difference and using them incorrectly can break stuff but that wasn’t the question you asked.

When it comes to preference I prefer to not use any hints at all unless I want to do minor tweaks to help out the compiler. Which is rare.

3 Likes

I also want to know where we should use ^js in shadow-cljs. Is it necessary or just optional?

^js helps Externs Inference. If you get warnings you can help the compiler by using the hint, if you don’t get any warnings you don’t need the hint. Things might work even if you leave out the hint despite getting warnings but sometimes it won’t.

See https://shadow-cljs.github.io/docs/UsersGuide.html#infer-externs

1 Like