Open
Description
Description
When I use next/navigation's redirect, the type of userId is successfully narrowed to string because if userId is undefined it calls redirect() whose return type is never.
But when I use redirect from createSharedPathnamesNavigation
, it's not working.
This is because of typescript's designed limitation. microsoft/TypeScript#36753
So I'm using redirect like this on my project. I re-declared redirect as a function not a const.
// navigation.ts
const navigation = createSharedPathnamesNavigation({
locales,
localePrefix,
});
export const { Link, usePathname, useRouter: useIntlRouter } = navigation;
export function redirect(...params: Parameters<typeof navigation.redirect>): never {
return navigation.redirect(...params);
}
I think it should work by default when using next-intl's redirect as next's default redirect does.
Mandatory reproduction URL (CodeSandbox or GitHub repository)
https://codesandbox.io/p/devbox/next-intl-bug-template-app-forked-fp6873
Reproduction description
Steps to reproduce:
- Open reproduction
- Click on the file ./src/app/[locale]/intl/page.tsx
- See error:
Type 'string | undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string'.
onuserId2
- But when you check
default/page.tsx
,intl2/page.tsx
, it works fine,
Expected behaviour
The type error should not be occurred like other files.