Skip to content

Instantly share code, notes, and snippets.

@AkinAguda
Last active July 31, 2023 12:20
Show Gist options
  • Save AkinAguda/155f0801036004340c6aff02f93cb1ba to your computer and use it in GitHub Desktop.
Save AkinAguda/155f0801036004340c6aff02f93cb1ba to your computer and use it in GitHub Desktop.
Force React component to expect certain props if another is defined
interface CommonData {
someRandomData: string;
}
type DataOne = {
someDataOneValue: string;
};
type DataTwo = {
someDataTwoValue: string;
};
type OneOrTwo =
| (DataTwo & Partial<Record<keyof DataOne, never>>)
| (DataOne & Partial<Record<keyof DataTwo, never>>);
type ComponentProps = OneOrTwo & CommonData;
const Component: React.FC<ComponentProps> = () => {
return <></>;
};
const Test: React.FC = () => {
return <Component someRandomData="" someDataOneValue="" />;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment