-
For the types
string
,number
,boolean
andfunction
, the!
shall be written explicitly.Example:
/**@type {!Array.<!function(!string):!boolean>}*/
to type an object as array of functions which require astring
value, notnull
orundefined
as argument and always return aboolean
value, nevernull
orundefined
Rationale: While for
string
,number
andboolean
types it is rather weird to have thenull
value, it is still better to write the default explicitely, to prevent errors like seen in Closure Compiler's own externs declarations and for consistenty with the rules 2) to 4).
(Agreed on in "coding rules for types" ml thread)
-
For the types
Object
,Array
andFunction
, the?
shall be written explicitly.Example:
/**@return {?Node}*/
to define that a method returns either an object of typeNode
or valuenull
Rationale: Null pointers can be a problem. The compiler checks the nullness when passing an object but not when accessing a member. Keeping awareness of nullness high is good.
(Agreed on in "coding rules for types" ml thread)
- Objects will always explicitly have
string
as the key. So each Object in a type definition starts withObject.<!string,
.
Rationale: Any other type makes no sense, because in JavaScript the key type is always a string
anyway.
(Agreed on in "coding rules for types" ml thread)
- Each non-constructor function must have a
@return
declaration.
Example: /**@return {undefined}*/
to define that a method returns always undefined
, which is also the default
Rationale: Being explicit about what a function returns avoids any ambiguity about whether the @return
declaration was just forgotten or what the exact default is
(Agreed on in "coding rules for types" ml thread)