I'm writing a Link component with TypeScript, and am trying to achieve a typed API that supports the varying parameters that my app routes use.
I have some routes defined like this:
enum Routes {
CONTACT = '/contact'
PRODUCT = '/product/[productId]'
}
type RouteParams = {
[Routes.CONTACT]: undefined;
[Routes.PRODUCT]: {
productId: string
}
}
The Link component API is like this:
type LinkProps = React.FC<{ href: Routes, params?: {} }>
My question is, is it possible for params here to dynamically change to the correct type defined in RouteParams, based on the input href value?
For example:
<Link href={Routes.CONTACT} /> // correctly typed
<Link href={Routes.PRODUCT} params={{}} /> // error: missing 'productId' param