I have a react component which either takes the to (react-router-dom prop) or href prop depending on where I want to redirect the user; whether it's within the app or to an external url. Typescript complains about my type with
Property 'to' does not exist on type 'LinkType'.
Property 'href' does not exist on type 'LinkType'.
This is how I defined my type
import React from 'react';
import { Link } from 'react-router-dom';
interface LinkBase {
label: string;
icon?: JSX.Element;
}
interface RouterLink extends LinkBase {
to: string;
}
interface HrefLink extends LinkBase {
href: string;
}
type LinkType = RouterLink | HrefLink;
export function NavLink(props: LinkType) {
const { to, label, icon, href } = props; // TS complains about those to and href
return(
{to ? (
<Link to={to}>{label}</Link>
) : (
<a href={href}>{label}</a>
)}
);
}