^js

special tagsince v1.10.238Edit

Add a type hint to a local binding name to prevent advanced compilation from munging property names on . dot calls— only if :infer-externs is enabled.

  • ^js - short form
  • ^js/React.Element - long form (purely for documentation, see details)

Details:

A ^js type hint on a local binding causes the ClojureScript compiler to generate externs for all subsequent property name accesses.

Technically, ^js is just as effective as the more specific ^js/Foo.Bar— because the full name is not actually a real Closure type. Closure plays it safe by refusing to mangle recognized property names (belonging to any type) being accessed on untyped objects.

Thus, the extra type info can be used for documentation, but has no real effect on your program.

Externs are generated as follows— but this really doesn't matter for CLJS, only in fully typed code:

  • ^js externs are added to Object.*
  • ^js/Foo.Bar externs are added to Foo.Bar.prototype.*

See https://code.thheller.com/blog/shadow-cljs/2017/11/06/improved-externs-inference.html.


Examples:

Given the following cljs code

(defn wrap-baz [^js/Foo.Bar x]
  (.baz x))

these externs will be created in inferred_externs.js:

var Foo = {};
Foo.Bar = function() {};
Foo.Bar.prototype.baz = function() {};

See Also: