Let's say I have a "user" object stored in redux, with fields for first name and last name (
interface User { firstName : string, lastName : string}
if using typescript). After retrieving a user from redux, I want to obtain the full name of the user by combining the first and last names: ${user.firstName} ${user.lastName}
. Currently, I have a function:
function getFullName(user: User) : string {
return `${user.firstName} ${user.lastName}`
}
In my application, I have several similar read-only functions like this one. I would prefer to have a method that can be called as user.getFullName()
or even user.fullName
. This way, I wouldn't need to import multiple functions separately or distinguish between properties and functions.
I could achieve this by creating class objects every time data is retrieved from redux and ensuring to use {...user}
when saving back to redux to convert it into a plain JavaScript object. However, this approach would introduce additional boilerplate code, especially when using typescript in combination with redux.
This issue seems like something that should have a straightforward solution. Is there a simple and efficient way to tackle this problem?