Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

155
Views
Typescript interface property want to change optional to required depending on other property

I have the following Typescript interface:

type animation = "Grow" | "Fade" | "Slide";

interface ISnackbarProps {
  open: boolean;
  autoHideDuration?: number;
  position?: position;
  variant?: variant;
  type?: AlertColor;
  animation?: animation;
  icon?: React.ReactElement;
  slideDirection?: slideDirection;
  action?: React.ReactElement;
  handleClose: () => void;
}

In the above interface, slideDirection is optional. But I want to update it optional to required only when the animation property has the "slide" value.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can achieve it with type. Here's an example:

type animation = "Grow" | "Fade";

type commonSnackBarProps = {
  open: boolean;
  autoHideDuration?: number;
  position?: position;
  variant?: variant;
  type?: AlertColor;
  icon?: React.ReactElement;
  action?: React.ReactElement;
  handleClose: () => void;
};

type SnackbarProps =
  | (commonSnackBarProps & {
      animation?: animation;
      slideDirection?: slideDirection;
    })
  | (commonSnackBarProps & {
      animation?: "Slide";
      slideDirection: slideDirection;
    });

You can check how this works in this sandbox.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!