How can I specify "any local pathname" as a prop with typedRoutes enabled? #83259
Unanswered
ravinggenius
asked this question in
Help
Replies: 4 comments 12 replies
-
(Sorry for all the edits. I was having trouble with the formatting. |
Beta Was this translation helpful? Give feedback.
0 replies
-
You can extract href type directly from link and use it like this " export type InternalHref = LinkProps["href"] " |
Beta Was this translation helpful? Give feedback.
3 replies
-
What would be a better type to pass to |
Beta Was this translation helpful? Give feedback.
2 replies
-
Could we take a step back? Is this what you want? import Link from "next/link";
import type { Route } from 'next'
export function MyLink({ href }: { href: Route }) {
return <Link href={href}>My Link</Link>
} |
Beta Was this translation helpful? Give feedback.
7 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
I've enabled
typedRoutes
. How can I wrapLink
without getting type errors on internal HREFs when using my custom component?Additional information
The
LinkProps
type takesRouteInferType
as a type argument (without restrictions) and passes it directly to__next_route_internal_types__.RouteImpl
.RouteImpl
in turn uses it likeHere's my styled anchor component. Passing
string
toLinkProps
works for defining the component...... but I still get an error (below) when I use it. The error is on the
href
prop name:(
locale
is typeen-US
, andplatformId
is typestring
.)I noticed when I cast the string template literal, the error goes away, and my code compiles and runs fine:
Is this my only option? It seems really hacky to have to cast all of my links. Interestingly I don't get an error if I use
Link
directly, even if I don't cast withInternalHref
:Example
I copied a minimal
Anchor
component to this gist.Beta Was this translation helpful? Give feedback.
All reactions