— Typescript, React — 1 min read
1<Component id={id} />
1<Component content={content} description={description} />
1interface CommonProps {2 index: number;3}45interface PropsWithId {6 id: string;7 content?: never;8 description?: never;9}1011interface PropsWithContent {12 id?: never;13 content: string;14 description: string;15}1617type ConditionalProps = PropsWithId | PropsWithContent;18type Props = CommonProps & ConditionalProps;1920const Component = ({ index, id, content, description }: Props) => (21 <div>22 <h1>I'm Component {index}</h1>23 {id && <p>id: {id}</p>}24 {content && <p>content: {content}</p>}25 {description && <p>description: {description}</p>}26 </div>27);2829export default function App() {30 return (31 <div className="App">32 <Component index={1} id="1" />33 <Component index={2} content="content" description="description" />34 <Component index={3} />35 </div>36 );37}