From cec384f01f9438ff518768d8dffe83ee273243f3 Mon Sep 17 00:00:00 2001 From: Bram Ceulemans Date: Wed, 6 Nov 2024 11:12:23 +0000 Subject: [PATCH 1/2] Fix type definition for route() with only options --- src/js/index.d.ts | 17 ++++++++++------- tests/js/route.test-d.ts | 10 +++++++++- 2 files changed, 19 insertions(+), 8 deletions(-) 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..a0be52b2 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,11 @@ 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')); +assertType( + route(undefined, undefined, undefined, { + url: 'http://localhost', + port: null, + defaults: {}, + routes: {}, + } as Config), +); From a1a22201fd6942aa4adf94059e703983f518cb8e Mon Sep 17 00:00:00 2001 From: Jacob Baker-Kretzmar Date: Sat, 9 Nov 2024 10:46:02 -0500 Subject: [PATCH 2/2] Add tests for other return types --- tests/js/route.test-d.ts | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/tests/js/route.test-d.ts b/tests/js/route.test-d.ts index a0be52b2..45257700 100644 --- a/tests/js/route.test-d.ts +++ b/tests/js/route.test-d.ts @@ -97,11 +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')); -assertType( - route(undefined, undefined, undefined, { - url: 'http://localhost', - port: null, - defaults: {}, - routes: {}, - } as Config), -); + +// Test route function return types +assertType(route('optional', { maybe: 'foo' })); +assertType(route('optional', 'foo')); +assertType(route(undefined, undefined, undefined, {} as Config));