The title sound confusing but here's the gist of it.
I have a User interface that may or may not have some properties depending on from where it's fetched. Here's what I mean: (notice the optional properties role and client_details)
export interface User {
id: string;
first_name: string;
last_name: string;
email: string;
// -- One of these in undefined depending on fetch location
role?: Role;
client_details?: ClientDetails;
}
The previous approach works, however having an undefined value every time doesn't look very pretty. It would be neeter if I could do this:
export interface User<ExtraType = Role | ClientDetails> {
id: string;
first_name: string;
last_name: string;
email: string;
// -- Dynamic property name instead, much cleaner approach
[ExtraType === Role ? 'role' : 'client_details']: ExtraType;
}
This doesn't work and gives a bunch of errors, mainly A computed property name cannot reference a type parameter from its containing type.
I'm aware I can simply make a static property extra & simply use it every time, but I wanted the property name to somewhat make sense. Is this feasible at all? Thank you!