Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Localized pathname type error when reached a certain number of objects #726

Open
m4rvelous252 opened this issue Dec 17, 2023 · 2 comments
Open
Labels
bug Something isn't working contributions welcome Good for people looking to contribute has-workaround upstream-issue This issue is caused by an upstream dependency (e.g. Next.js)

Comments

@m4rvelous252
Copy link

Description

I'm implementing localized pathname approach with the doc when I add the language switcher that change the locale of the current page, the router.replace method would not accept the pathname variables that was returned by usePathname(). Further investigation shows it stops erroring once I reduce the number of routes in my localized pathname object, might be a typescript compiling issue 🤣 Anyway, please take a look and at the recreation repo below on the branch "next-intl-issue". If you add another route the pathname in navigation.ts typescript will error out in Switcher.tsx. In this repo the threshold is 26, but in my other project it's 24, that website has a dynamic route in the pathname.

Mandatory reproduction URL (CodeSandbox or GitHub repository)

https://github.com/m4rvelous252/next-demo/tree/next-intl-issue

Reproduction description

Steps to reproduce:

  1. Open reproduction
  2. Add an additional route to pathnames in navigation.ts
  3. See type error in Switcher.tsx

Expected behaviour

Would be nice if typescript behave correctly here

@m4rvelous252 m4rvelous252 added bug Something isn't working unconfirmed Needs triage. labels Dec 17, 2023
@amannn
Copy link
Owner

amannn commented Dec 18, 2023

Thanks for the report! Just found this, there seems to be a limitation in TypeScript: microsoft/TypeScript#40803. When generating a union based on a type, the number of elements is limited to 25 :-/.

You can theoretically disable type checking and allow arbitrary strings for localized pathnames like this:

export const { Link, redirect, usePathname, useRouter, getPathname } =
  createLocalizedPathnamesNavigation<typeof locales, Record<string, string>>({
    locales,
    pathnames
  });

I'd be curious if there's a better solution in case someone more experienced with TypeScript comes across this.

Any chance you could use dynamic params to reduce the number of pathnames (e.g. /product/[id])?

@amannn amannn added contributions welcome Good for people looking to contribute has-workaround and removed unconfirmed Needs triage. labels Dec 18, 2023
@m4rvelous252
Copy link
Author

Currently, I'm putting a @ts-expect-error flag above this since I'm only using it in the language switcher component, I still want to use the auto complete from next-intl (super nice btw).

  const lang = useLocale();
  const pathname = usePathname();
  const router = useRouter();
  const params = useParams();

  const handleSwitchLanguage = (lang: string) => {
    // @ts-expect-error
    router.replace({ pathname, params }, { locale: lang });
  };

Would be nice if there's another way to get around this 👍

@amannn amannn added the upstream-issue This issue is caused by an upstream dependency (e.g. Next.js) label Mar 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working contributions welcome Good for people looking to contribute has-workaround upstream-issue This issue is caused by an upstream dependency (e.g. Next.js)
Projects
None yet
Development

No branches or pull requests

2 participants