I want to create a discriminated union where all objects are of another type.
say i have a type
type PersonNames = 'john' | 'kate'
type Person = {
name: PersonNames,
favorites: unknown // can be any object
}
and i want to create a PersonPreferences type that is a union of Person objects such that only PersonNames is allowed for name...
type PersonPreferences =
| {
name: 'john'.
favorites: {
food: 'pizza'
}
| {
name: 'kate',
favorites: {
book: 'horror'
}
How can this be accomplished?
I tried this but it allows for arbitrary names
type PersonPreferences = Person & (
| {
name: 'john'.
favorites: {
food: 'pizza'
}
| {
name: 'jimmy', // should error
favorites: {
book: 'science'
}
)