opral / inlang-paraglide-js

Tree-shakable i18n library build on the inlang ecosystem.
https://inlang.com/m/gerre34r/library-inlang-paraglideJs
33 stars 0 forks source link

[Paraglide-Next] Support statically typed links #174

Closed coderfin closed 1 month ago

coderfin commented 1 month ago

As a NextJS dev using Paraglide-Next it would be nice to have support for statically typed links.

NextJS has a typedRoutes feature that can be enabled to support statically typed links. As stated in the NextJS documentation this helps to:

prevent typos and other errors when using next/link, improving type safety when navigating between pages

https://nextjs.org/docs/app/building-your-application/configuring/typescript#statically-typed-links https://nextjs.org/docs/app/api-reference/next-config-js/typedRoutes

The href type is currently:

href: string | import("url").UrlObject;

The NextJS docs suggest:

href: Route<T> | URL
coderfin commented 1 month ago

I have a workaround but it would be nice not to have to create a new component to import.

import { Link as I18nLink } from '@/lib/i18n'
import type { Route } from 'next'

export default function Link<T extends string>({
  href,
  children,
  ...rest
}: {
  href: Route<T> | URL
} & React.ComponentPropsWithoutRef<typeof I18nLink>) {
  return (
    <I18nLink href={href} {...rest}>
      {children}
    </I18nLink>
  )
}
LorisSigrist commented 1 month ago

+1 This should absolutely be supported

LorisSigrist commented 1 month ago

I've been able to get it to work by using typeof import("next/link").default as a type-annotation internally instead of inlining the type. However, the typescript compiler insists on inlining it anyways, I still need to find a workaround there.