I have a few React components that need exactly two of three props to represent a time range: from
, to
, or duration
. Any combination of two is enough to derive the third one correctly. So I want TypeScript to enforce that exactly two of these props is required.
I tried defining props like this:
type RangeProps =
| {
from: Date
to: Date
}
| {
from: Date
duration: number
}
| {
duration: number
to: Date
}
But when I supply all three props (<Range from={startDate} duration={duration} to={endDate} />
, TypeScript doesn't complain. How can I get it to complain?