Skip to content

Commit 9cdd300

Browse files
Merge pull request #862 from pataar/better_typing
feat(typescript): add branded type for creating routes
2 parents 8e286c1 + 3414896 commit 9cdd300

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

src/js/index.d.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ type RouteName = KnownRouteName | (string & {});
2323
// which would prevent intellisense from autocompleting known route names.
2424
// See https://stackoverflow.com/a/61048124/6484459.
2525

26+
/**
27+
* A generated route URL string.
28+
*/
29+
export type RouteUrl = string;
30+
2631
/**
2732
* A valid route name to pass to `route()` to generate a URL.
2833
*/
@@ -201,14 +206,14 @@ export function route<T extends ValidRouteName>(
201206
params?: RouteParams<T> | undefined,
202207
absolute?: boolean,
203208
config?: Config,
204-
): string;
209+
): RouteUrl;
205210

206211
export function route<T extends ValidRouteName>(
207212
name: T,
208213
params?: ParameterValue | undefined,
209214
absolute?: boolean,
210215
config?: Config,
211-
): string;
216+
): RouteUrl;
212217

213218
/**
214219
* Ziggy's Vue plugin.

tests/js/route.test-d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { assertType } from 'vitest';
2-
import { Config, route, Router } from '../../src/js';
2+
import { Config, route, Router, RouteUrl } from '../../src/js';
33

44
// Add generated routes to use for testing inside this file. In a real app these declarations
55
// would be in a separate file generated by running `php artisan ziggy:generate --types`.
@@ -111,6 +111,7 @@ assertType(route().current('posts.comments.show', 'foo'));
111111
// Test route function return types
112112
assertType<string>(route('optional', { maybe: 'foo' }));
113113
assertType<string>(route('optional', 'foo'));
114+
assertType<RouteUrl>(route('posts.comments.show', 'foo'));
114115
assertType<Router>(route(undefined, undefined, undefined, {} as Config));
115116

116117
// Uncomment to test strict route name checking - invalid route names in this file should error

0 commit comments

Comments
 (0)