Skip to content

Latest commit

 

History

History
18 lines (12 loc) · 1.28 KB

no-mutable-array.md

File metadata and controls

18 lines (12 loc) · 1.28 KB

no-mutable-array

The --fix option on the command line automatically fixes problems reported by this rule.

Requires use of $ReadOnlyArray instead of just Array or array shorthand notation. $ReadOnlyArray is immutable array collection type and the superclass of Array and tuple types in Flow. Use of $ReadOnlyArray instead of Array can solve some "problems" in typing with Flow (e.g., 1, 2).

General reasons for using immutable data structures:

  • They are simpler to construct, test, and use
  • They help to avoid temporal coupling
  • Their usage is side-effect free (no defensive copies)
  • Identity mutability problem is avoided
  • They always have failure atomicity
  • They are much easier to cache

Note that initialization of a variable with an empty array is considered valid (e.g., const values: Array<string> = [];). This behavior resembles the behavior of Flow's unsealed objects, as it is assumed that empty array is intended to be mutated.