I ran into a very annoying problem is that I get a bunch of typescript errors, while trying to conditionally render the Next.js Link component or a button element. So if the href prop is passed, I want to render Next.js built-in Link component, else it should be Button. This is just one example of the error
Type 'HTMLAnchorElement' is missing the following properties from type 'HTMLButtonElement': disabled, form, formAction, formEnctype, and 11 more.ts(2322)
I don't know how to make typescript understand that please use (ButtonAsLinkProps & aProps) if the href prop is present, and use ButtonAsButtonProps if not.
I am very new to typescript, trying to work with it for only few weeks, pretty sure, that the types here are wrong.
This is my first post here, I am so desperate, can't find any topics related to this anywhere on the internet. Would really appreciate any help. The code for the component is bellow:
interface BaseProps {
children: ReactNode
primary?: boolean
className?: string
}
type ButtonAsButtonProps = BaseProps &
Omit<DetailedHTMLProps<ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>, keyof BaseProps> & {
href: undefined
}
type ButtonAsLinkProps = BaseProps & LinkProps
type aProps = Omit<AnchorHTMLAttributes<HTMLAnchorElement>, keyof BaseProps>
type ButtonProps = (ButtonAsLinkProps & aProps) | ButtonAsButtonProps
const Button = ({
children,
className,
href,
primary,
as,
replace,
scroll,
shallow,
passHref,
prefetch,
locale,
legacyBehavior,
...rest
}: ButtonProps): JSX.Element | undefined => {
const classNames = `${commonStyles} ${primary ? primaryStyles : secondaryStyles} ${className}`
const linkProps = {
as,
replace,
scroll,
shallow,
passHref,
prefetch,
locale,
legacyBehavior,
}
if (href && linkProps) {
return (
<Link href={href} {...linkProps}>
<a {...rest}>{children}</a>
</Link>
)
}
if (!href) {
return (
<button {...rest} className={classNames}>
{children}
</button>
)
}
}
export default Button