Conditionally required types

Someone that has any ideas on how I can make everything required if accepted is true, if it's false then the other 3 fields are not required.
type GeoLocation = {
accepted: boolean;
accuracy?: number;
longitude?: number;
latitude?: number;
};
type GeoLocation = {
accepted: boolean;
accuracy?: number;
longitude?: number;
latitude?: number;
};
3 Replies
batata
batata2y ago
this logic seems off
Brendonovich
Brendonovich2y ago
How's this?
type GeoLocation = {
accepted: true;
accuracy: number;
longitude: number;
latitude: number;
} | {
accepted: false;
accuracy?: number;
longitude?: number;
latitude?: number;
}
type GeoLocation = {
accepted: true;
accuracy: number;
longitude: number;
latitude: number;
} | {
accepted: false;
accuracy?: number;
longitude?: number;
latitude?: number;
}
Could also move accuracy, longitude and latitude into a separate type and Partial it for the accepted: false case
barry
barry2y ago
I would rather overload here than this but yeh
Want results from more Discord servers?
Add your server