#Conditionally required fields

4 messages · Page 1 of 1 (latest)

forest tusk
#

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;
};
serene lantern
#
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

forest tusk