Props12 / 12
Up next

Discriminated-union props

// a discriminant field narrows the allowed shape
type AlertProps =
  | { kind: "info"; message: string }
  | { kind: "error"; message: string; code: number };

function Alert(props: AlertProps) {
  if (props.kind === "error") {
    return <p>Error {props.code}: {props.message}</p>;
  }
  return <p>{props.message}</p>;
}

export default Alert;
What you're solving

model variant props with a tagged union