Tengo la siguiente interfaz mecanografiada:
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; } En la interfaz anterior, slideDirection es opcional. Pero quiero actualizarlo opcional a requerido solo cuando la propiedad de animation tiene el valor "slide" .
Puedes lograrlo con type . Aquí hay un ejemplo:
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; });Puede comprobar cómo funciona esto en este sandbox .