Skip to content

Commit

Permalink
Fix #202 (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
tatethurston authored Oct 8, 2024
1 parent 0516c38 commit 01b9efd
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 17 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
4 changes: 2 additions & 2 deletions examples/app/@types/nextjs-routes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<NextAppRouterInstance, 'push' | 'replace' | 'href'> & {
push(href: StaticRoute | RouteLiteral, options?: NavigateOptions): void;
Expand Down Expand Up @@ -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 = <Pathname extends Route["pathname"] = Route["pathname"]>() => RoutedQuery<Pathname>;
export const useParams: <Pathname extends Route["pathname"] = Route["pathname"]>() => RoutedQuery<Pathname>;
}
4 changes: 2 additions & 2 deletions packages/e2e/@types/nextjs-routes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<NextAppRouterInstance, 'push' | 'replace' | 'href'> & {
push(href: StaticRoute | RouteLiteral, options?: NavigateOptions): void;
Expand Down Expand Up @@ -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 = <Pathname extends Route["pathname"] = Route["pathname"]>() => RoutedQuery<Pathname>;
export const useParams: <Pathname extends Route["pathname"] = Route["pathname"]>() => RoutedQuery<Pathname>;
}
7 changes: 7 additions & 0 deletions packages/e2e/typetest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ nextRoutes({ foo: false });
// eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/no-empty-function
function expectType<T>(_value: T) {}

// eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/no-empty-function
function expectNotAny<T>(_value: NotAny<T>) {}

type NotAny<T> = unknown extends T ? never : T;

// next/link

// Links with string hrefs
Expand Down Expand Up @@ -350,7 +355,9 @@ expectType<(href: string, options: PrefetchOptions) => void>(
useAppRouter().prefetch,
);

expectNotAny(usePathname());
expectType<RouteLiteral>(usePathname());

expectNotAny(useParams());
expectType<RoutedQuery>(useParams());
expectType<RoutedQuery<"/bars/[bar]">>(useParams<"/bars/[bar]">());
2 changes: 1 addition & 1 deletion packages/nextjs-routes/package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
Expand Down
20 changes: 10 additions & 10 deletions packages/nextjs-routes/src/__snapshots__/core.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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<NextAppRouterInstance, 'push' | 'replace' | 'href'> & {
push(href: StaticRoute | RouteLiteral, options?: NavigateOptions): void;
Expand Down Expand Up @@ -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 = <Pathname extends Route["pathname"] = Route["pathname"]>() => RoutedQuery<Pathname>;
export const useParams: <Pathname extends Route["pathname"] = Route["pathname"]>() => RoutedQuery<Pathname>;
}
",
],
Expand Down Expand Up @@ -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<NextAppRouterInstance, 'push' | 'replace' | 'href'> & {
push(href: StaticRoute | RouteLiteral, options?: NavigateOptions): void;
Expand Down Expand Up @@ -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 = <Pathname extends Route["pathname"] = Route["pathname"]>() => RoutedQuery<Pathname>;
export const useParams: <Pathname extends Route["pathname"] = Route["pathname"]>() => RoutedQuery<Pathname>;
}
",
],
Expand Down Expand Up @@ -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<NextAppRouterInstance, 'push' | 'replace' | 'href'> & {
push(href: StaticRoute | RouteLiteral, options?: NavigateOptions): void;
Expand Down Expand Up @@ -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 = <Pathname extends Route["pathname"] = Route["pathname"]>() => RoutedQuery<Pathname>;
export const useParams: <Pathname extends Route["pathname"] = Route["pathname"]>() => RoutedQuery<Pathname>;
}
",
],
Expand Down Expand Up @@ -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<NextAppRouterInstance, 'push' | 'replace' | 'href'> & {
push(href: StaticRoute | RouteLiteral, options?: NavigateOptions): void;
Expand Down Expand Up @@ -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 = <Pathname extends Route["pathname"] = Route["pathname"]>() => RoutedQuery<Pathname>;
export const useParams: <Pathname extends Route["pathname"] = Route["pathname"]>() => RoutedQuery<Pathname>;
}
",
],
Expand Down Expand Up @@ -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<NextAppRouterInstance, 'push' | 'replace' | 'href'> & {
push(href: StaticRoute | RouteLiteral, options?: NavigateOptions): void;
Expand Down Expand Up @@ -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 = <Pathname extends Route["pathname"] = Route["pathname"]>() => RoutedQuery<Pathname>;
export const useParams: <Pathname extends Route["pathname"] = Route["pathname"]>() => RoutedQuery<Pathname>;
}
",
],
Expand Down
4 changes: 2 additions & 2 deletions packages/nextjs-routes/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<NextAppRouterInstance, 'push' | 'replace' | 'href'> & {
push(href: StaticRoute | RouteLiteral, options?: NavigateOptions): void;
Expand Down Expand Up @@ -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 = <Pathname extends Route["pathname"] = Route["pathname"]>() => RoutedQuery<Pathname>;
export const useParams: <Pathname extends Route["pathname"] = Route["pathname"]>() => RoutedQuery<Pathname>;
}
`;
}
Expand Down

0 comments on commit 01b9efd

Please sign in to comment.