I
I
Ivan2021-11-02 17:43:57
typescript
Ivan, 2021-11-02 17:43:57

Conditions when creating interface\type typescript?

How to specify that if there is no icon, then there must be text and vice versa?

type IProps = {
data: { icon?: React.ReactSVGElement; text?:string; id:string | number}[];
handleClick: (id: string | number) => void;
};

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WbICHA, 2021-11-02
@WblCHA

type IProps = {
  data: (({ icon: any; } | { text: string; }) & { id: string | number })[];
};

Or in the normal
type IProps = {
  data: Data[];
}; 

type Data = Data.WithIcon | Data.WithText;
namespace Data {
  interface Base {
    id: string | number;
  }

  export interface WithIcon extends Base {
    icon: any;
  }

  export interface WithText extends Base {
    text: string;
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question