Skip to content

Compile Time Type Checking

David Nolen edited this page Aug 9, 2015 · 22 revisions

ClojureScript can easily leverage Google Closure for compile time type checking. This is done simply via ClojureScript docstrings which are compiled into JSDoc style comment blocks. Google Closure supports many useful kinds of type annotation. For example you can write a function that takes non-nullable Object.

(defn foo 
  "@param {!Object} x
  [x] x)

If Closure can infer that some invoke may pass null, this will result in a compile time type error.

Type checking can be enabled for :simple or :advanced optimization builds:

(require '[cljs.build.api :as b])

(b/build "src"
  {:main 'my.ns
   :output-to "app.js"
   :optimizations :simple
   :verbose true
   :closure-warnings
   {:check-types :error ;; << ADD THIS
    :undefined-names :off
    :externs-validation :off
    :missing-properties :off}})

(System/exit 0)

The other :closure-warnings options here are to disable noisier checks that are less relevant for ClojureScript projects.

Protocols

Protocols all emit an @interface annotation. Any deftype or defrecord that implements some protocol will have it's JSDoc annotation extended with @implement for that protocol.

(defn bar
  "@param {IVector}"
  [x] x)
Clone this wiki locally