google / ground-platform

Ground hosted components: Web console, Cloud Functions, db config
http://groundplatform.org
Apache License 2.0
206 stars 88 forks source link

[Code health] Create mixin to allow immutable data objects to be copied with changes #1038

Open gino-m opened 1 year ago

gino-m commented 1 year ago

Based on https://mauricereigman.medium.com/immutable-typescript-classes-with-object-copywith-typesafe-a84fff3971dc:

copyWith(newValues: { [P in keyof this]?: this[P] }): this {
      return Object.assign(Object.create(this.constructor.prototype), { ...this, ...newValues });
}
gino-m commented 11 months ago

We currently use inheritance. We could instead do something like this https://medium.com/@saif.adnan/typescript-mixin-ee962be3224d