I have a scenario where I want to enhance a record (plain Javascript object) of arrays with additional properties/methods, ideally by instantiating a new class:
class Dataframe extends Object {
_nrow: number;
_ncol: number;
_identity: number[];
constructor(values: Record<string, any[]>) {
super();
this._nrow = values[Object.keys(values)[0]].length;
this._ncol = Object.keys(values).length;
this._identity = Array(this._nrow).fill(1);
Object.assign(this, values);
}
// Additional methods can be added here...
}
While this approach works, using Object.assign()
causes me to lose the typings on values
:
const rawData = {x : [1, 2, 3, 4], y: [5, 6, 7, 8]}
const data = new Dataframe(rawData)
data.x // Property 'x' does not exist on type 'Dataframe'
I would like to know if there is a method for the class instance to inherit the properties from the values
object or perhaps an alternate solution to this issue?