#Conditionally required fields
4 messages · Page 1 of 1 (latest)
type GeoFalse = {
accepted: false;
accuracy?: number;
longitude?: number;
latitude?: number;
};
type GeoTrue = {
accepted: true;
accuracy: number;
longitude: number;
latitude: number;
};
type GeoLocation = GeoTrue | GeoFalse
You can then even go as far as to remove all the conditional properties from the GeoFalse type, meaning that if accepted == false it will refuse any other fields
I did this instead:
type GeoLocation =
| {
accepted: true;
accuracy: number;
longitude: number;
latitude: number;
}
| {
accepted: false;
accuracy?: number;
longitude?: number;
latitude?: number;
};