Repository: tatethurston/nextjs-routes
Pull request #81 (2022-10-16 11:29:12)

Export Locales as a type
Extracted types to an exported type declaration 

Asamsig: I actually suspected the `Locales` => `Locale` was coming, I realized after the fact, that it should probably have been in singular form.

I dropped the conditional export, the conditional import, and now always use it as the type for `locale`.

Happy to contribute to this really great library!

Patch:
diff --git a/examples/intl/nextjs-routes.d.ts b/examples/intl/nextjs-routes.d.ts
index 5a41861..52906fc 100644
--- a/examples/intl/nextjs-routes.d.ts
+++ b/examples/intl/nextjs-routes.d.ts
@@ -16,6 +16,11 @@ declare module "nextjs-routes" {
 
   export type RoutedQuery<P extends Route["pathname"]> = QueryForPathname[P];
 
+  export type Locale = 
+      | "en-US"
+      | "fr"
+      | "nl-NL";
+  
   /**
    * A typesafe utility function for generating paths in your application.
    *
@@ -36,9 +41,7 @@ declare module "next/link" {
   export interface LinkProps extends Omit<NextLinkProps, "href" | "locale"> {
     href: RouteOrQuery;
     locale?: 
-      | "en-US"
-      | "fr"
-      | "nl-NL"
+      | Locale
       | false;
   }
 
@@ -59,7 +62,7 @@ declare module "next/link" {
 
 // prettier-ignore
 declare module "next/router" {
-  import type { Route, RoutedQuery } from "nextjs-routes";
+  import type { Locale, Route, RoutedQuery } from "nextjs-routes";
   import type { NextRouter as Router } from "next/dist/client/router";
   export * from "next/dist/client/router";
   export { default } from "next/dist/client/router";
@@ -68,9 +71,7 @@ declare module "next/router" {
 
   interface TransitionOptions extends Omit<NextTransitionOptions, 'locale'> {
     locale?: 
-      | "en-US"
-      | "fr"
-      | "nl-NL"
+      | Locale
       | false;
   };
 
@@ -85,10 +86,7 @@ declare module "next/router" {
     > {
     defaultLocale: "en-US";
     domainLocales?: undefined;
-    locale: 
-      | "en-US"
-      | "fr"
-      | "nl-NL";
+    locale: Locale;
     locales: [
       "en-US",
       "fr",
diff --git a/examples/intl/package-lock.json b/examples/intl/package-lock.json
index 1b36efc..d33ba5b 100644
--- a/examples/intl/package-lock.json
+++ b/examples/intl/package-lock.json
@@ -1,11 +1,11 @@
 {
-  "name": "src-pages",
+  "name": "intl",
   "version": "0.0.1",
   "lockfileVersion": 2,
   "requires": true,
   "packages": {
     "": {
-      "name": "src-pages",
+      "name": "intl",
       "version": "0.0.1",
       "dependencies": {
         "next": "12.2.0",
diff --git a/examples/typescript/types/nextjs-routes.d.ts b/examples/typescript/types/nextjs-routes.d.ts
index 5b01784..31907d2 100644
--- a/examples/typescript/types/nextjs-routes.d.ts
+++ b/examples/typescript/types/nextjs-routes.d.ts
@@ -18,6 +18,8 @@ declare module "nextjs-routes" {
 
   export type RoutedQuery<P extends Route["pathname"]> = QueryForPathname[P];
 
+  export type Locale = undefined;
+  
   /**
    * A typesafe utility function for generating paths in your application.
    *
@@ -57,7 +59,7 @@ declare module "next/link" {
 
 // prettier-ignore
 declare module "next/router" {
-  import type { Route, RoutedQuery } from "nextjs-routes";
+  import type { Locale, Route, RoutedQuery } from "nextjs-routes";
   import type { NextRouter as Router } from "next/dist/client/router";
   export * from "next/dist/client/router";
   export { default } from "next/dist/client/router";
@@ -79,7 +81,7 @@ declare module "next/router" {
     > {
     defaultLocale?: undefined;
     domainLocales?: undefined;
-    locale?: undefined;
+    locale?: Locale;
     locales?: undefined;
     pathname: P;
     push(
diff --git a/src/core.ts b/src/core.ts
index a14023b..2522d7b 100644
--- a/src/core.ts
+++ b/src/core.ts
@@ -99,6 +99,9 @@ function generate(routes: Route[], config: NextJSRoutesOptions): string {
   };
   const unknownQueryParamsType =
     "{ [key: string]: string | string[] | undefined }";
+  const locales = `\n      | ${i18n.locales
+    .map((x) => `"${x}"`)
+    .join("\n      | ")}`;
   return `\
 // THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
 // This file will be automatically regenerated when your Next.js server is running.
@@ -130,6 +133,8 @@ declare module "nextjs-routes" {
 
   export type RoutedQuery<P extends Route["pathname"]> = QueryForPathname[P];
 
+  export type Locale = ${!i18n.locales.length ? "undefined" : locales};
+  
   /**
    * A typesafe utility function for generating paths in your application.
    *
@@ -150,11 +155,7 @@ declare module "next/link" {
   export interface LinkProps extends Omit<NextLinkProps, "href" | "locale"> {
     href: RouteOrQuery;
     locale?: ${
-      !i18n.locales.length
-        ? "false"
-        : `\n      | ${[...i18n.locales.map((x) => `"${x}"`), false].join(
-            "\n      | "
-          )}`
+      !i18n.locales.length ? "false" : `\n      | Locale\n      | false`
     };
   }
 
@@ -175,7 +176,7 @@ declare module "next/link" {
 
 // prettier-ignore
 declare module "next/router" {
-  import type { Route, RoutedQuery } from "nextjs-routes";
+  import type { Locale, Route, RoutedQuery } from "nextjs-routes";
   import type { NextRouter as Router } from "next/dist/client/router";
   export * from "next/dist/client/router";
   export { default } from "next/dist/client/router";
@@ -184,11 +185,7 @@ declare module "next/router" {
 
   interface TransitionOptions extends Omit<NextTransitionOptions, 'locale'> {
     locale?: ${
-      !i18n.locales.length
-        ? "false"
-        : `\n      | ${[...i18n.locales.map((x) => `"${x}"`), false].join(
-            "\n      | "
-          )}`
+      !i18n.locales.length ? "false" : `\n      | Locale\n      | false`
     };
   };
 
@@ -207,11 +204,7 @@ declare module "next/router" {
     domainLocales${
       i18n.domains?.length ? `: ${print(i18n.domains, 4)}` : "?: undefined"
     };
-    locale${
-      !i18n.locales.length
-        ? "?: undefined"
-        : `: \n      | ${i18n.locales.map((x) => `"${x}"`).join("\n      | ")}`
-    };
+    locale${(!i18n.locales.length ? "?: " : ": ") + "Locale"};
     locales${
       i18n.locales.length ? `: ${print(i18n.locales, 4)}` : "?: undefined"
     };


Test patch:
diff --git a/src/__snapshots__/core.test.ts.snap b/src/__snapshots__/core.test.ts.snap
index 7538ce2..203b5f3 100644
--- a/src/__snapshots__/core.test.ts.snap
+++ b/src/__snapshots__/core.test.ts.snap
@@ -21,6 +21,11 @@ declare module "nextjs-routes" {
 
   export type RoutedQuery<P extends Route["pathname"]> = QueryForPathname[P];
 
+  export type Locale = 
+      | "en-US"
+      | "fr"
+      | "nl-NL";
+  
   /**
    * A typesafe utility function for generating paths in your application.
    *
@@ -41,9 +46,7 @@ declare module "next/link" {
   export interface LinkProps extends Omit<NextLinkProps, "href" | "locale"> {
     href: RouteOrQuery;
     locale?: 
-      | "en-US"
-      | "fr"
-      | "nl-NL"
+      | Locale
       | false;
   }
 
@@ -64,7 +67,7 @@ declare module "next/link" {
 
 // prettier-ignore
 declare module "next/router" {
-  import type { Route, RoutedQuery } from "nextjs-routes";
+  import type { Locale, Route, RoutedQuery } from "nextjs-routes";
   import type { NextRouter as Router } from "next/dist/client/router";
   export * from "next/dist/client/router";
   export { default } from "next/dist/client/router";
@@ -73,9 +76,7 @@ declare module "next/router" {
 
   interface TransitionOptions extends Omit<NextTransitionOptions, 'locale'> {
     locale?: 
-      | "en-US"
-      | "fr"
-      | "nl-NL"
+      | Locale
       | false;
   };
 
@@ -103,10 +104,7 @@ declare module "next/router" {
         "http": true
       }
     ];
-    locale: 
-      | "en-US"
-      | "fr"
-      | "nl-NL";
+    locale: Locale;
     locales: [
       "en-US",
       "fr",
@@ -155,6 +153,8 @@ declare module "nextjs-routes" {
 
   export type RoutedQuery<P extends Route["pathname"]> = QueryForPathname[P];
 
+  export type Locale = undefined;
+  
   /**
    * A typesafe utility function for generating paths in your application.
    *
@@ -194,7 +194,7 @@ declare module "next/link" {
 
 // prettier-ignore
 declare module "next/router" {
-  import type { Route, RoutedQuery } from "nextjs-routes";
+  import type { Locale, Route, RoutedQuery } from "nextjs-routes";
   import type { NextRouter as Router } from "next/dist/client/router";
   export * from "next/dist/client/router";
   export { default } from "next/dist/client/router";
@@ -216,7 +216,7 @@ declare module "next/router" {
     > {
     defaultLocale?: undefined;
     domainLocales?: undefined;
-    locale?: undefined;
+    locale?: Locale;
     locales?: undefined;
     pathname: P;
     push(
@@ -263,6 +263,8 @@ declare module "nextjs-routes" {
 
   export type RoutedQuery<P extends Route["pathname"]> = QueryForPathname[P];
 
+  export type Locale = undefined;
+  
   /**
    * A typesafe utility function for generating paths in your application.
    *
@@ -302,7 +304,7 @@ declare module "next/link" {
 
 // prettier-ignore
 declare module "next/router" {
-  import type { Route, RoutedQuery } from "nextjs-routes";
+  import type { Locale, Route, RoutedQuery } from "nextjs-routes";
   import type { NextRouter as Router } from "next/dist/client/router";
   export * from "next/dist/client/router";
   export { default } from "next/dist/client/router";
@@ -324,7 +326,7 @@ declare module "next/router" {
     > {
     defaultLocale?: undefined;
     domainLocales?: undefined;
-    locale?: undefined;
+    locale?: Locale;
     locales?: undefined;
     pathname: P;
     push(
@@ -369,6 +371,8 @@ declare module "nextjs-routes" {
 
   export type RoutedQuery<P extends Route["pathname"]> = QueryForPathname[P];
 
+  export type Locale = undefined;
+  
   /**
    * A typesafe utility function for generating paths in your application.
    *
@@ -408,7 +412,7 @@ declare module "next/link" {
 
 // prettier-ignore
 declare module "next/router" {
-  import type { Route, RoutedQuery } from "nextjs-routes";
+  import type { Locale, Route, RoutedQuery } from "nextjs-routes";
   import type { NextRouter as Router } from "next/dist/client/router";
   export * from "next/dist/client/router";
   export { default } from "next/dist/client/router";
@@ -430,7 +434,7 @@ declare module "next/router" {
     > {
     defaultLocale?: undefined;
     domainLocales?: undefined;
-    locale?: undefined;
+    locale?: Locale;
     locales?: undefined;
     pathname: P;
     push(
@@ -475,6 +479,8 @@ declare module "nextjs-routes" {
 
   export type RoutedQuery<P extends Route["pathname"]> = QueryForPathname[P];
 
+  export type Locale = undefined;
+  
   /**
    * A typesafe utility function for generating paths in your application.
    *
@@ -514,7 +520,7 @@ declare module "next/link" {
 
 // prettier-ignore
 declare module "next/router" {
-  import type { Route, RoutedQuery } from "nextjs-routes";
+  import type { Locale, Route, RoutedQuery } from "nextjs-routes";
   import type { NextRouter as Router } from "next/dist/client/router";
   export * from "next/dist/client/router";
   export { default } from "next/dist/client/router";
@@ -536,7 +542,7 @@ declare module "next/router" {
     > {
     defaultLocale?: undefined;
     domainLocales?: undefined;
-    locale?: undefined;
+    locale?: Locale;
     locales?: undefined;
     pathname: P;
     push(
@@ -598,6 +604,8 @@ declare module "nextjs-routes" {
 
   export type RoutedQuery<P extends Route["pathname"]> = QueryForPathname[P];
 
+  export type Locale = undefined;
+  
   /**
    * A typesafe utility function for generating paths in your application.
    *
@@ -637,7 +645,7 @@ declare module "next/link" {
 
 // prettier-ignore
 declare module "next/router" {
-  import type { Route, RoutedQuery } from "nextjs-routes";
+  import type { Locale, Route, RoutedQuery } from "nextjs-routes";
   import type { NextRouter as Router } from "next/dist/client/router";
   export * from "next/dist/client/router";
   export { default } from "next/dist/client/router";
@@ -659,7 +667,7 @@ declare module "next/router" {
     > {
     defaultLocale?: undefined;
     domainLocales?: undefined;
-    locale?: undefined;
+    locale?: Locale;
     locales?: undefined;
     pathname: P;
     push(