Skip to content

Default & Initial

Anton edited this page Aug 7, 2019 · 1 revision

The difference between default and initial attributes on a property is the following:

  • default is used for records, such as configs, and will set the property to be optional which means that in addition to specified types, it will be able to take the undefined type.
  • initial is used for interfaces and constructors, which will initialise the property in their constructor to the given initial value, however the property is not optional anymore, since it will always be present on the type.

For example, given the following types:

<types>
  <type record name="ConfigRecord">
    <prop type="?string" name="test" default="null" />
  </type>
  <type name="Config">
    <prop type="?string" name="test" default="null" />
  </type>
  <type interface name="SomeType">
    <prop type="?string" name="test" initial="null" />
  </type>
</types>

The next externs will be generated:

/* typal wiki/Default-&-Initial/types.xml externs */
/**
 * @record
 */
var ConfigRecord
/**
 * @type {(?string)|undefined}
 */
ConfigRecord.prototype.test
/**
 * @typedef {{ test: ((?string)|undefined) }}
 */
var Config
/**
 * @interface
 */
var SomeType
/**
 * @type {?string}
 */
SomeType.prototype.test

Whereas the default can be set both on records, and primitive records ({}), it does not really make sense to set initial on the latter since they will never have a chance to initialise the initial variable.

Clone this wiki locally