Skip to content

Instantly share code, notes, and snippets.

@thepuskar
Created April 8, 2024 17:47
Show Gist options
  • Save thepuskar/1a9f8d88ca61bf3f91fdb803af49946d to your computer and use it in GitHub Desktop.
Save thepuskar/1a9f8d88ca61bf3f91fdb803af49946d to your computer and use it in GitHub Desktop.
TypeScript conditional types based on property value
type IProps = {
children: React.ReactNode;
asChild?: boolean;
} & (IModalProp | IRedirectProp);
interface IModalProp {
mode: "modal";
}
interface IRedirectProp {
mode: "redirect";
redirectUrl: string;
}
// Modal usage:
<MyComponent asChild mode="modal">Some content</MyComponent>
// Redirection usage:
<MyComponent mode="redirect" redirectUrl="/some-page">Rerouting...</MyComponent>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment