entity-state
entity-state copied to clipboard
Flag to enforce immutable entities
Is it useful to add a flag in the super
call of your store that enforces immutable entities? The object containing the data is immutable but the entities itself aren't required to be immutable.
This could simply be checked by adding current !== updated
after this.onUpdate
and throwing an error depending on the result.
Also: what should the default value for the flag be, if it's wanted. Or no default value?
You could use the DeepReadonly<T>
typescript type (available since 2.8) to make this a design time check. ie.
abstract onUpdate(current: DeepReadonly<T>, updated: Partial<T>): T;
potentially even:
abstract onUpdate(current: DeepReadonly<T>, updated: DeepReadonly<Partial<T>>): T;