I am faced with a User interface structured as follows
interface IUser {
name: string,
phoneNumber: string
}
and another interface called PublicUser structured like this
interface IPublicUser {
name: string
}
The goal is to extract only public information from an object of type IUser
.
I attempted the following approach:
const user: IUser = {
name: 'Eliott',
phoneNumber : '+1...'
}
console.log(user as unknown as IPublicUser)
However, it consistently returns the full user object.
To solve this issue temporarily, I created a method called toPublicJson
to selectively return the desired field. However, this solution is not ideal since any changes in my interface will necessitate updating this method as well.
Do you have any suggestions or advice?
Thank you