I'm currently exploring methods to define a second class that mirrors the fields of the first one. It seems that this may not be feasible, as I discovered during compilation time that class fields are not allowed. Even at runtime, utilizing Object.keys
for fields is only achievable after initialization and would return defined values.
Situation:
class Entity {
public id!: string;
public uuid!: string;
public firstname!: string;
public lastname!: string;
}
Now, I aim to introduce weights
(potentially requiring minor adjustments on client code).
Logically, I either need to include weights
within the same class if feasible or in a separate similar class like:
class EntityWeights {
public id!: number;
public uuid!: number;
public firstname!: number;
public lastname!: number;
}
Subsequently, it would be essential to manage both classes to maintain consistent code (although not ideal).
If I opt for modifying solely the Entity
class to incorporate weights, I would proceed as follows:
class Entity {
public id!: [string, number];
public uuid!: [string, number];
public firstname!: [string, number];
public lastname!: [string, number];
}
Upon attempting this, it became evident that in client code - where I pass const entity = new Entity({___})
to other segments such as putInDB(entity)
- I encountered difficulties refactoring to exclusively utilize either weights or values.
How would you approach this scenario?