Skip to content

Commit d3d7cb0

Browse files
Merge pull request #787 from tighten/strict-route-checks
Add ability to strictly type check route names
2 parents 8970524 + c6299b5 commit d3d7cb0

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/js/index.d.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
*/
66
export interface RouteList {}
77

8+
/**
9+
* Marker interface to configure Ziggy's type checking behavior.
10+
*/
11+
export interface TypeConfig {}
12+
813
/**
914
* A route name registered with Ziggy.
1015
*/
@@ -13,7 +18,9 @@ type KnownRouteName = keyof RouteList;
1318
/**
1419
* A route name, or any string.
1520
*/
16-
type RouteName = KnownRouteName | (string & {});
21+
type RouteName = TypeConfig extends { strictRouteNames: true }
22+
? KnownRouteName
23+
: KnownRouteName | (string & {});
1724
// `(string & {})` prevents TypeScript from reducing this type to just `string`,
1825
// which would prevent intellisense from autocompleting known route names.
1926
// See https://stackoverflow.com/a/61048124/6484459.

tests/js/route.test-d.ts

+7
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,10 @@ assertType(route().current('posts.comments.show', 'foo'));
102102
assertType<string>(route('optional', { maybe: 'foo' }));
103103
assertType<string>(route('optional', 'foo'));
104104
assertType<Router>(route(undefined, undefined, undefined, {} as Config));
105+
106+
// Uncomment to test strict route name checking - invalid route names in this file should error
107+
// declare module '../../src/js' {
108+
// interface TypeConfig {
109+
// strictRouteNames: true;
110+
// }
111+
// }

0 commit comments

Comments
 (0)