Dependence the type of one prop on the type of other prop

Advertisements

Is it possible to somehow combine the description of the two types of the React component props below.

On isEdit == true:

type DialogProps = {
  isEdit: boolean
  obj: ICustom
}

and on isEdit == false:

type DialogProps = {
  isEdit: boolean
  obj: null
}

>Solution :

Try using discriminated union:

type EditDialogProps = {isEdit: true, obj: ICustom};
type NonEditDialogProps = {isEdit: false}; // just remove obj in this one :)
type DialogProps = EditDialogProps | NonEditDialogProps;

Leave a ReplyCancel reply