Skip to content

Commit

Permalink
Merge pull request #787 from tighten/strict-route-checks
Browse files Browse the repository at this point in the history
Add ability to strictly type check route names
  • Loading branch information
bakerkretzmar authored Nov 9, 2024
2 parents 8970524 + c6299b5 commit d3d7cb0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/js/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
*/
export interface RouteList {}

/**
* Marker interface to configure Ziggy's type checking behavior.
*/
export interface TypeConfig {}

/**
* A route name registered with Ziggy.
*/
Expand All @@ -13,7 +18,9 @@ type KnownRouteName = keyof RouteList;
/**
* A route name, or any string.
*/
type RouteName = KnownRouteName | (string & {});
type RouteName = TypeConfig extends { strictRouteNames: true }
? KnownRouteName
: KnownRouteName | (string & {});
// `(string & {})` prevents TypeScript from reducing this type to just `string`,
// which would prevent intellisense from autocompleting known route names.
// See https://stackoverflow.com/a/61048124/6484459.
Expand Down
7 changes: 7 additions & 0 deletions tests/js/route.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,10 @@ assertType(route().current('posts.comments.show', 'foo'));
assertType<string>(route('optional', { maybe: 'foo' }));
assertType<string>(route('optional', 'foo'));
assertType<Router>(route(undefined, undefined, undefined, {} as Config));

// Uncomment to test strict route name checking - invalid route names in this file should error
// declare module '../../src/js' {
// interface TypeConfig {
// strictRouteNames: true;
// }
// }

0 comments on commit d3d7cb0

Please sign in to comment.