Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix type definition for route() with only options #786

Merged
merged 2 commits into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));