Repository: tatethurston/nextjs-routes
Pull request #25 (2022-07-08 17:54:29)

re-export from `next/link` and `next/router`
Now that types are exported from `next/link` and `next/router` we need to re-export all types that we don't override from Next itself or they cannot be used.


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 056846a..769964b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,9 @@
 # Changelog
 
+## 0.0.17
+
+- re-export types from `next/link` and `next/router`
+
 ## 0.0.16
 
 - fixed prettier as an optional peer dependency
diff --git a/src/utils.ts b/src/utils.ts
index 2390963..52f1cad 100644
--- a/src/utils.ts
+++ b/src/utils.ts
@@ -135,6 +135,7 @@ declare module "next/link" {
   import type { Route } from "nextjs-routes";
   import type { LinkProps as NextLinkProps } from "next/dist/client/link";
   import type { PropsWithChildren, MouseEventHandler } from "react";
+  export * from "next/dist/client/link";
 
   type RouteOrQuery = Route | { query?: { [key: string]: string | undefined } };
 
@@ -160,7 +161,8 @@ declare module "next/link" {
 declare module "next/router" {
   import type { Route } from "nextjs-routes";
   import type { NextRouter as Router } from "next/dist/client/router";
-  export { RouterEvent } from "next/dist/client/router";
+  export * from "next/dist/client/router";
+  export { default } from "next/dist/client/router";
 
   type TransitionOptions = Parameters<Router["push"]>[2];
 


Test patch:
diff --git a/e2e/nextjs-routes.d.ts b/e2e/nextjs-routes.d.ts
index 85e9c70..cfdc1ee 100644
--- a/e2e/nextjs-routes.d.ts
+++ b/e2e/nextjs-routes.d.ts
@@ -14,6 +14,7 @@ declare module "next/link" {
   import type { Route } from "nextjs-routes";
   import type { LinkProps as NextLinkProps } from "next/dist/client/link";
   import type { PropsWithChildren, MouseEventHandler } from "react";
+  export * from "next/dist/client/link";
 
   type RouteOrQuery = Route | { query?: { [key: string]: string | undefined } };
 
@@ -39,7 +40,8 @@ declare module "next/link" {
 declare module "next/router" {
   import type { Route } from "nextjs-routes";
   import type { NextRouter as Router } from "next/dist/client/router";
-  export { RouterEvent } from "next/dist/client/router";
+  export * from "next/dist/client/router";
+  export { default } from "next/dist/client/router";
 
   type TransitionOptions = Parameters<Router["push"]>[2];
 
diff --git a/e2e/typetest.tsx b/e2e/typetest.tsx
index 61d3850..7914c2c 100644
--- a/e2e/typetest.tsx
+++ b/e2e/typetest.tsx
@@ -1,5 +1,6 @@
 import Link from "next/link";
-import { useRouter, RouterEvent } from "next/router";
+import { LinkProps } from "next/link";
+import { useRouter, RouterEvent, NextRouter } from "next/router";
 import type { Route } from "nextjs-routes";
 
 // eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/no-empty-function
@@ -45,6 +46,15 @@ function expectType<T>(_value: T) {}
 // @ts-expect-error replace typo
 <Link href={{ query: {} }} replacey />;
 
+// LinkProps
+
+// ensure LinkProps is our LinkProps, not the untyped one
+
+expectType<LinkProps["href"]>({ pathname: "/" });
+
+// @ts-expect-error invalid pathname
+expectType<LinkProps["href"]>({ pathname: "/invalid" });
+
 // next/router
 const router = useRouter();
 
@@ -133,6 +143,17 @@ routerEvent = "routeChangeStart";
 // @ts-expect-error event typo
 routerEvent = "routeChangeStarty";
 
+// NextRouter
+
+// ensure NextRouter is our NextRouter, not the untyped one
+
+let nextRouter: NextRouter = undefined as unknown as NextRouter;
+
+nextRouter.push({ pathname: "/" });
+
+// @ts-expect-error invalid pathname
+nextRouter.push({ pathname: "/invalid" });
+
 // nextjs-routes
 
 // Route
diff --git a/src/__snapshots__/test.ts.snap b/src/__snapshots__/test.ts.snap
index 2b9af95..aacfada 100644
--- a/src/__snapshots__/test.ts.snap
+++ b/src/__snapshots__/test.ts.snap
@@ -33,6 +33,7 @@ declare module \\"next/link\\" {
   import type { Route } from \\"nextjs-routes\\";
   import type { LinkProps as NextLinkProps } from \\"next/dist/client/link\\";
   import type { PropsWithChildren, MouseEventHandler } from \\"react\\";
+  export * from \\"next/dist/client/link\\";
 
   type RouteOrQuery = Route | { query?: { [key: string]: string | undefined } };
 
@@ -58,7 +59,8 @@ declare module \\"next/link\\" {
 declare module \\"next/router\\" {
   import type { Route } from \\"nextjs-routes\\";
   import type { NextRouter as Router } from \\"next/dist/client/router\\";
-  export { RouterEvent } from \\"next/dist/client/router\\";
+  export * from \\"next/dist/client/router\\";
+  export { default } from \\"next/dist/client/router\\";
 
   type TransitionOptions = Parameters<Router[\\"push\\"]>[2];