Skip to content

Commit

Permalink
Merge pull request #786 from bram-pkg/bram-pkg/fix-type-definition
Browse files Browse the repository at this point in the history
Fix type definition for route() with only options
  • Loading branch information
bakerkretzmar authored Nov 9, 2024
2 parents 11d61d0 + a1a2220 commit d379ade
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
17 changes: 10 additions & 7 deletions src/js/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,26 +168,29 @@ 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<T extends RouteName>(
name: T,
params?: RouteParams<T> | undefined,
absolute?: boolean,
config?: Config,
): string;

export function route<T extends RouteName>(
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.
Expand Down
7 changes: 6 additions & 1 deletion tests/js/route.test-d.ts
Original file line number Diff line number Diff line change
@@ -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`.
Expand Down Expand Up @@ -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<string>(route('optional', { maybe: 'foo' }));
assertType<string>(route('optional', 'foo'));
assertType<Router>(route(undefined, undefined, undefined, {} as Config));

0 comments on commit d379ade

Please sign in to comment.