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

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.


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.


1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.