Migrating to v3.22 #1566
-
Today I noticed there are some breaking changes in v3.22 while I was trying to upgrade I have a component like this where I pass the <LinkButton
as={Link} // <-- this is the important part!
icon={faMagnifyingGlass}
href="/search-by-invoice-no"
text={t("search-by-invoice-no")}
size="sm"
/> It's using these types type LinkProps = {
href: string,
prefetch?: boolean,
scroll?: boolean,
className?: string,
children: ReactNode,
};
type LinkButtonProps = {
as: (props: LinkProps) => ReactNode,
variant?: ButtonVariant,
size?: "lg" | "sm",
icon?: IconProp,
href: string,
text?: string,
disabled?: boolean,
stretched?: boolean,
}; It worked perfectly within the next.js ecosystem until now. But now it complains about the type of Types of property 'href' are incompatible.
Type 'string' is not assignable to type '"/login" | "/register" | "/terms" | "/" | "/admin" | ({ pathname: "/login"; } & Omit<UrlObject, "pathname">) | ({ pathname: "/register"; } & Omit<UrlObject, "pathname">) | ... 13 more ... | ({ ...; } & Omit<...>)'. First I tried to solve it by extending those types with generics. It worked. But it didn't work well with next.js' Link component. Is there any way to disable type-safe href? or any idea to solve this? Btw I noticed the Link composition section in the docs. But it's not an option for this case. Because this component comes from a library which is supposed to stay agnostic with next.js plugins. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
You mean updating to But in regard to your question: If you use You can compose the |
Beta Was this translation helpful? Give feedback.
You mean updating to
createNavigation
requires some changes, right? Version 3.22 is by itself just a minor version with full backwards compatibility, so you can upgrade at your own pace if desired. The upcoming v4 will eventually removed deprecated navigation APIs in favor ofcreateNavigation
though.But in regard to your question: If you use
pathnames
, theLink
component fromcreateNavigation
will help you to make sure that only known pathnames can be received as anhref
. While for simple pathnames without any params this is just a typed string, in case you support pathnames with params (e.g./users/[userId]
) this will require passing the params separatel…