My problem is pretty trivial, though I can't find an appropriate solution.
I have the following interface:
export interface User {
id: number;
...
}
And method
getCurrentUser(): Observable<User> {
return this.http.get<User>('url');
}
Okay, now, I want to extend a user object, that getCurrentUser
method returns with additional methods.
First thing that came to my mind is to create a decorator, something like this
export class UserDecorator implements User {
id: number;
constructor(private user: User) {}
someMethod() {
...
}
}
Apparently, I have to use it like so
.pipe(map((user: User) => new UserDecorator(user)))
What I don't really like in this solution, is
- I have to copy/paste all
User
properties toUserDecorator
, declaringUser
interface as class instead, to avoid copy/pasting, is not a good solution as well - Setting
User
object as a constructor argument result in the following problems- I have to leave it as it is, and access
User
properties through additional member (e.g.userDecorator.user.id
) which is not looking good - I have to manually copy all the values from
User
toUserDecorator
in the constructor.
- I have to leave it as it is, and access
Does my concerns make sense? Is there some better solution, or at least, some conventional solution among Angular community for that problem?
Thank you.