-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Open
Description
type Pet = {|
petId: string
|}
type Car = {|
carId: string
|}
type Union = Pet | Car
This causes an error:
function getId(arg: Union): string {
return arg.petId || arg.carId
}
This doesn't work either:
function getId(arg: Union): string {
let id
if (arg.petId) {
id = arg.petId
} else if (arg.carId) {
id = arg.carId
}
return id
}
This works, but is a very convoluted amount of typing given I have told Flow that there will 100% surely be either a carId or petId attribute of type string.
function getId(arg: Union): string {
let id
if (arg.petId) {
id = arg.petId
} else if (arg.carId) {
id = arg.carId
} else {
throw new Error('This is pointless')
}
return id
}
Is there a 'right way' to do this? If not, aren't these disjoint unions very limited in their ability?
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels