Either/Or Props in TypeScript (opens in new tab)
Sometimes in TypeScript we’d like to say a function can either have one typed prop, or the other, never both and never neither. This can be achieved with a union type and type never: type Props = { markdown: string, copy?: never } | { markdown?: never, copy: string} }; const component = ({ markdown, copy }: Props) => markdown ? parseMarkdown(markdown) : <>copy ; console.log(component({ markdown: "### Important" })) // Important console.log(component({ copy: "Just information" })) /...
Read the original article