Compile Time Type Checking

This page documents on-going development on the
ClojureScript master branch.

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:

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

Type Resolution

ClojureScript supports unqualified type names in @param and @return
annotations. These will automatically be expanded based on the vars
currently in scope.

(defn bar
"@param {!IVector} v
@return {!IMap}"
[v] {})

Both IVector and IMap will be resolved to cljs.core where they are
defined.

Protocols

Programs written around concrete types are not idiomatic. ClojureScript
itself is written in terms of a rich set of protocols. Therefore it is
important to support type checking on protocols rather than particular
types.

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

Future Enhancements

Pre & Post conditions

It would be interesting to generate @param and @return
automatically. For example the following: