Im trying to extend my component props conditionally depending on if a particular prop is passed.
My aim is to extend props by attributes of an anchor
if a href
prop is passed, and extend by attributes of a button
if not.
Is this even possible?
Here's my attempt:
Can't you just define properties as union type and use typeguard? Something like this:
type ButtonProps = { onClick: () => {} }
type LinkProps = { href: string }
type BaseProps = {
spinner?: boolean
}
type Props = BaseProps & (ButtonProps | LinkProps)
export const Button = (props: Props) => {
if ('href' in props) {
// Link
} else {
// Button
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With