I have an array called Users[]
which holds class instances of User
. I create this array by mapping over query results like so:
function GetUserByUserData(data : UserData[] | ReadonlyArray<UserData>) : User[]
{
return data.map((userData) => {
return new User(UserData);
});
}
Note: UserData
is an object of type {id: number; [key: string]: any;}
.
Then, I use a method to create rows based on some class properties like so:
//User class
export default class User
{
private _data: UserData;
get data()
{
return this._data;
}
}
let rows = Users.map((user) => makeRowFromUser(user));
function makeRowFromUser(user: User)
{
console.log(user); //logs {_data}
console.log(user.data); //undefined
console.log(user._data); //logs data normally
}
I realize since _data
is private I shouldn’t be able to access it using user._data
.
Why does this happen? Does array.map
destructure the Users[]
array? Does the user
argument get destructured before being passed to the function? And since this is unintended behavior on my part, is there a way to prevent this from happening so I can pass the User
class instance as a class instance?