Is it possible to have something like this?
{
roles: contains "admin",
admin_details: string
} && {
roles: contains "vendor",
vendor_details: string
}
?
I figure probably not. What would be the cleanest way to get this sort of behavior, where a type has details depending on what roles they have?
The only way I could think of it was having something like:
({
is_admin: true,
admin_details: string
} |
{ is_admin: false,
admin_details: undefined
})
&& ({
is_vendor: true,
vendor_details: string
} |
{ is_vendor: false,
vendor_details: undefined
});
And adding those additional is_role field on role creation/change. But this is pretty verbose. Is it a good solution, or would you recommend something else?
Thanks!
edit: maybe a union between Admin and Vendor interfaces, with a type guard that examines array membership would be cleaner?
edit: I attempted to use the code in the first answer, but am having issues. See the updated playground here
another edit (too long for comments): playground here for failure when adding a third type to the RolesMap when using the Type Guard solution, see line 35