Immutable<T>:T extends PrimitiveType ? T : T extends AtomicObject ? T : T extends ReadonlyMap<infer K, infer V> ? ReadonlyMap<Immutable<K>, Immutable<V>> : T extends ReadonlySet<infer V> ? ReadonlySet<Immutable<V>> : T extends WeakReferences ? T : T extends object ? { readonly [K in keyof T]: Immutable<T[K]> } : T
Immutable<T> is used to flag and enforce immutability of a
RootState and its descendants - typically values assigned to a
Store (@watchable/store!Store#write) or retrieved from it
(@watchable/store!Store#read). This tells the compiler that no
modification should be made anywhere in a Store's state tree.
Unlike some implementations (such as Object.freeze, Object.seal or
Immutable.js) our compile-time Immutability
approach introduces no special objects and methods and doesn't require you to
change your runtime code at all.
Under the hood, Immutable<T> is a recursive implementation of Typescript's
Readonly<T>. Primitive properties are already immutable by definition.
Functions are treated as primitive values. All other objects and arrays and
their descendants are made Readonly recursively until they hit a primitive
'leaf' value.
Relying on Typescript's builtin Readonly allows the use of normal
javascript values and syntax, with the exception that operations which would
manipulate the item are disallowed by the compiler. Applications written in
Typescript get the greatest benefit from this approach, but javascript IDEs
that load typings for code-completion can also indicate violations of the
Readonly contract.
Immutable<T>
is used to flag and enforce immutability of a RootState and its descendants - typically values assigned to a Store (@watchable/store!Store#write) or retrieved from it (@watchable/store!Store#read). This tells the compiler that no modification should be made anywhere in a Store's state tree.Unlike some implementations (such as Object.freeze, Object.seal or Immutable.js) our compile-time Immutability approach introduces no special objects and methods and doesn't require you to change your runtime code at all.
Under the hood,
Immutable<T>
is a recursive implementation of Typescript'sReadonly<T>
. Primitive properties are already immutable by definition. Functions are treated as primitive values. All other objects and arrays and their descendants are madeReadonly
recursively until they hit a primitive 'leaf' value.Relying on Typescript's builtin
Readonly
allows the use of normal javascript values and syntax, with the exception that operations which would manipulate the item are disallowed by the compiler. Applications written in Typescript get the greatest benefit from this approach, but javascript IDEs that load typings for code-completion can also indicate violations of theReadonly
contract.