diff --git a/src/js/index.d.ts b/src/js/index.d.ts index a8ef6833..7de7a638 100644 --- a/src/js/index.d.ts +++ b/src/js/index.d.ts @@ -168,6 +168,15 @@ interface Router { */ // Called with no arguments - returns a Router instance export function route(): Router; + +// Called with configuration arguments only - returns a configured Router instance +export function route( + name: undefined, + params: undefined, + absolute?: boolean, + config?: Config, +): Router; + // Called with a route name and optional additional arguments - returns a URL string export function route( name: T, @@ -175,19 +184,13 @@ export function route( absolute?: boolean, config?: Config, ): string; + export function route( name: T, params?: ParameterValue | undefined, absolute?: boolean, config?: Config, ): string; -// Called with configuration arguments only - returns a configured Router instance -export function route( - name: undefined, - params: undefined, - absolute?: boolean, - config?: Config, -): Router; /** * Ziggy's Vue plugin. diff --git a/tests/js/route.test-d.ts b/tests/js/route.test-d.ts index 7d6ca792..45257700 100644 --- a/tests/js/route.test-d.ts +++ b/tests/js/route.test-d.ts @@ -1,5 +1,5 @@ import { assertType } from 'vitest'; -import { route } from '../../src/js'; +import { Config, route, Router } from '../../src/js'; // Add generated routes to use for testing inside this file. In a real app these declarations // would be in a separate file generated by running `php artisan ziggy:generate --types`. @@ -97,3 +97,8 @@ assertType(route().current('posts.comments.show', { post: 2 })); assertType(route().current('posts.comments.show', 2)); // assertType(route().current('posts.comments.show', [2])); // TODO shouldn't error, only one required param assertType(route().current('posts.comments.show', 'foo')); + +// Test route function return types +assertType(route('optional', { maybe: 'foo' })); +assertType(route('optional', 'foo')); +assertType(route(undefined, undefined, undefined, {} as Config));