From 01b9efd732424e52a88ee8c244941057de25c561 Mon Sep 17 00:00:00 2001 From: Tate Thurston Date: Mon, 7 Oct 2024 17:40:03 -0700 Subject: [PATCH] Fix #202 (#203) --- CHANGELOG.md | 4 ++++ examples/app/@types/nextjs-routes.d.ts | 4 ++-- packages/e2e/@types/nextjs-routes.d.ts | 4 ++-- packages/e2e/typetest.tsx | 7 +++++++ packages/nextjs-routes/package.json | 2 +- .../src/__snapshots__/core.test.ts.snap | 20 +++++++++---------- packages/nextjs-routes/src/core.ts | 4 ++-- 7 files changed, 28 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 029a9d7..7994638 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 2.2.3 + +- Bug fix: `usePathname` and `useParams` were incorrectly resolving to `any` return types. + ## 2.2.2 - Adds support for Next.js's `app` directory. `Link` accepts either static routes (no url parameters) or a `RouteLiteral` string, which can be generated by the `route` helper from this library: diff --git a/examples/app/@types/nextjs-routes.d.ts b/examples/app/@types/nextjs-routes.d.ts index c014538..c615559 100644 --- a/examples/app/@types/nextjs-routes.d.ts +++ b/examples/app/@types/nextjs-routes.d.ts @@ -192,7 +192,7 @@ declare module "next/navigation" { * * Read more: [Next.js Docs: `usePathname`](https://nextjs.org/docs/app/api-reference/functions/use-pathname) */ - export const usePathname = () => RouteLiteral; + export const usePathname: () => RouteLiteral; type AppRouterInstance = Omit & { push(href: StaticRoute | RouteLiteral, options?: NavigateOptions): void; @@ -237,5 +237,5 @@ declare module "next/navigation" { * * Read more: [Next.js Docs: `useParams`](https://nextjs.org/docs/app/api-reference/functions/use-params) */ - export const useParams = () => RoutedQuery; + export const useParams: () => RoutedQuery; } diff --git a/packages/e2e/@types/nextjs-routes.d.ts b/packages/e2e/@types/nextjs-routes.d.ts index 0b81773..841f5ae 100644 --- a/packages/e2e/@types/nextjs-routes.d.ts +++ b/packages/e2e/@types/nextjs-routes.d.ts @@ -194,7 +194,7 @@ declare module "next/navigation" { * * Read more: [Next.js Docs: `usePathname`](https://nextjs.org/docs/app/api-reference/functions/use-pathname) */ - export const usePathname = () => RouteLiteral; + export const usePathname: () => RouteLiteral; type AppRouterInstance = Omit & { push(href: StaticRoute | RouteLiteral, options?: NavigateOptions): void; @@ -239,5 +239,5 @@ declare module "next/navigation" { * * Read more: [Next.js Docs: `useParams`](https://nextjs.org/docs/app/api-reference/functions/use-params) */ - export const useParams = () => RoutedQuery; + export const useParams: () => RoutedQuery; } diff --git a/packages/e2e/typetest.tsx b/packages/e2e/typetest.tsx index 76ad937..a14688c 100644 --- a/packages/e2e/typetest.tsx +++ b/packages/e2e/typetest.tsx @@ -25,6 +25,11 @@ nextRoutes({ foo: false }); // eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/no-empty-function function expectType(_value: T) {} +// eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/no-empty-function +function expectNotAny(_value: NotAny) {} + +type NotAny = unknown extends T ? never : T; + // next/link // Links with string hrefs @@ -350,7 +355,9 @@ expectType<(href: string, options: PrefetchOptions) => void>( useAppRouter().prefetch, ); +expectNotAny(usePathname()); expectType(usePathname()); +expectNotAny(useParams()); expectType(useParams()); expectType>(useParams<"/bars/[bar]">()); diff --git a/packages/nextjs-routes/package.json b/packages/nextjs-routes/package.json index 5a55d84..da55a09 100644 --- a/packages/nextjs-routes/package.json +++ b/packages/nextjs-routes/package.json @@ -1,6 +1,6 @@ { "name": "nextjs-routes", - "version": "2.2.2", + "version": "2.2.3", "description": "Type safe routing for Next.js", "license": "MIT", "author": "Tate ", diff --git a/packages/nextjs-routes/src/__snapshots__/core.test.ts.snap b/packages/nextjs-routes/src/__snapshots__/core.test.ts.snap index 4eedbb9..8ab97a9 100644 --- a/packages/nextjs-routes/src/__snapshots__/core.test.ts.snap +++ b/packages/nextjs-routes/src/__snapshots__/core.test.ts.snap @@ -203,7 +203,7 @@ declare module "next/navigation" { * * Read more: [Next.js Docs: \`usePathname\`](https://nextjs.org/docs/app/api-reference/functions/use-pathname) */ - export const usePathname = () => RouteLiteral; + export const usePathname: () => RouteLiteral; type AppRouterInstance = Omit & { push(href: StaticRoute | RouteLiteral, options?: NavigateOptions): void; @@ -248,7 +248,7 @@ declare module "next/navigation" { * * Read more: [Next.js Docs: \`useParams\`](https://nextjs.org/docs/app/api-reference/functions/use-params) */ - export const useParams = () => RoutedQuery; + export const useParams: () => RoutedQuery; } ", ], @@ -452,7 +452,7 @@ declare module "next/navigation" { * * Read more: [Next.js Docs: \`usePathname\`](https://nextjs.org/docs/app/api-reference/functions/use-pathname) */ - export const usePathname = () => RouteLiteral; + export const usePathname: () => RouteLiteral; type AppRouterInstance = Omit & { push(href: StaticRoute | RouteLiteral, options?: NavigateOptions): void; @@ -497,7 +497,7 @@ declare module "next/navigation" { * * Read more: [Next.js Docs: \`useParams\`](https://nextjs.org/docs/app/api-reference/functions/use-params) */ - export const useParams = () => RoutedQuery; + export const useParams: () => RoutedQuery; } ", ], @@ -702,7 +702,7 @@ declare module "next/navigation" { * * Read more: [Next.js Docs: \`usePathname\`](https://nextjs.org/docs/app/api-reference/functions/use-pathname) */ - export const usePathname = () => RouteLiteral; + export const usePathname: () => RouteLiteral; type AppRouterInstance = Omit & { push(href: StaticRoute | RouteLiteral, options?: NavigateOptions): void; @@ -747,7 +747,7 @@ declare module "next/navigation" { * * Read more: [Next.js Docs: \`useParams\`](https://nextjs.org/docs/app/api-reference/functions/use-params) */ - export const useParams = () => RoutedQuery; + export const useParams: () => RoutedQuery; } ", ], @@ -951,7 +951,7 @@ declare module "next/navigation" { * * Read more: [Next.js Docs: \`usePathname\`](https://nextjs.org/docs/app/api-reference/functions/use-pathname) */ - export const usePathname = () => RouteLiteral; + export const usePathname: () => RouteLiteral; type AppRouterInstance = Omit & { push(href: StaticRoute | RouteLiteral, options?: NavigateOptions): void; @@ -996,7 +996,7 @@ declare module "next/navigation" { * * Read more: [Next.js Docs: \`useParams\`](https://nextjs.org/docs/app/api-reference/functions/use-params) */ - export const useParams = () => RoutedQuery; + export const useParams: () => RoutedQuery; } ", ], @@ -2284,7 +2284,7 @@ declare module "next/navigation" { * * Read more: [Next.js Docs: \`usePathname\`](https://nextjs.org/docs/app/api-reference/functions/use-pathname) */ - export const usePathname = () => RouteLiteral; + export const usePathname: () => RouteLiteral; type AppRouterInstance = Omit & { push(href: StaticRoute | RouteLiteral, options?: NavigateOptions): void; @@ -2329,7 +2329,7 @@ declare module "next/navigation" { * * Read more: [Next.js Docs: \`useParams\`](https://nextjs.org/docs/app/api-reference/functions/use-params) */ - export const useParams = () => RoutedQuery; + export const useParams: () => RoutedQuery; } ", ], diff --git a/packages/nextjs-routes/src/core.ts b/packages/nextjs-routes/src/core.ts index a595799..cd91afd 100644 --- a/packages/nextjs-routes/src/core.ts +++ b/packages/nextjs-routes/src/core.ts @@ -333,7 +333,7 @@ declare module "next/navigation" { * * Read more: [Next.js Docs: \`usePathname\`](https://nextjs.org/docs/app/api-reference/functions/use-pathname) */ - export const usePathname = () => RouteLiteral; + export const usePathname: () => RouteLiteral; type AppRouterInstance = Omit & { push(href: StaticRoute | RouteLiteral, options?: NavigateOptions): void; @@ -378,7 +378,7 @@ declare module "next/navigation" { * * Read more: [Next.js Docs: \`useParams\`](https://nextjs.org/docs/app/api-reference/functions/use-params) */ - export const useParams = () => RoutedQuery; + export const useParams: () => RoutedQuery; } `; }