Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 7 additions & 0 deletions .changeset/resolve-accept-query-hash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@sveltejs/kit': patch
---

feat: allow `resolve()` to accept pathnames with query strings and hash fragments

Adds a `ResolvablePath` type that extends `Pathname` to also accept `?${string}` and `#${string}` suffixes. This allows usage like `goto(resolve('/map?x=1'))` which satisfies both the type checker and the `svelte/no-navigation-without-resolve` lint rule.
4 changes: 2 additions & 2 deletions packages/kit/src/runtime/app/paths/client.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @import { Asset, RouteId, Pathname, ResolvedPathname } from '$app/types' */
/** @import { Asset, RouteId, Pathname, ResolvablePath, ResolvedPathname } from '$app/types' */
/** @import { ResolveArgs } from './types.js' */
import { base, assets, hash_routing } from './internal/client.js';
import { resolve_route } from '../../../utils/routing.js';
Expand Down Expand Up @@ -47,7 +47,7 @@ const pathname_prefix = hash_routing ? '#' : '';
* ```
* @since 2.26
*
* @template {RouteId | Pathname} T
* @template {RouteId | ResolvablePath} T
* @param {ResolveArgs<T>} args
* @returns {ResolvedPathname}
*/
Expand Down
4 changes: 2 additions & 2 deletions packages/kit/src/runtime/app/paths/public.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RouteId, Pathname, ResolvedPathname } from '$app/types';
import { RouteId, ResolvablePath, ResolvedPathname } from '$app/types';
import { ResolveArgs } from './types.js';

export { resolve, asset, match } from './client.js';
Expand All @@ -24,6 +24,6 @@ export let assets: '' | `https://${string}` | `http://${string}` | '/_svelte_kit
/**
* @deprecated Use [`resolve(...)`](https://svelte.dev/docs/kit/$app-paths#resolve) instead
*/
export function resolveRoute<T extends RouteId | Pathname>(
export function resolveRoute<T extends RouteId | ResolvablePath>(
...args: ResolveArgs<T>
): ResolvedPathname;
4 changes: 2 additions & 2 deletions packages/kit/src/runtime/app/paths/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Pathname, RouteId, RouteParams } from '$app/types';
import { ResolvablePath, RouteId, RouteParams } from '$app/types';

export type ResolveArgs<T extends RouteId | Pathname> = T extends RouteId
export type ResolveArgs<T extends RouteId | ResolvablePath> = T extends RouteId
? RouteParams<T> extends Record<string, never>
? [route: T]
: [route: T, params: RouteParams<T>]
Expand Down
10 changes: 10 additions & 0 deletions packages/kit/src/types/ambient.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,16 @@ declare module '$app/types' {
*/
export type Pathname = ReturnType<AppTypes['Pathname']>;

/**
* A pathname that may optionally include a query string and/or hash fragment.
* Used as input to the `resolve` function.
*/
export type ResolvablePath =
| Pathname
| `${Pathname}?${string}`
| `${Pathname}#${string}`
| `${Pathname}?${string}#${string}`;

/**
* `Pathname`, but possibly prefixed with a base path. Used for `page.url.pathname`.
*/
Expand Down
18 changes: 14 additions & 4 deletions packages/kit/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3124,7 +3124,7 @@ declare module '$app/navigation' {
}

declare module '$app/paths' {
import type { RouteId, Pathname, ResolvedPathname, RouteParams, Asset } from '$app/types';
import type { RouteId, ResolvablePath, ResolvedPathname, RouteParams, Asset, Pathname } from '$app/types';
/**
* A string that matches [`config.kit.paths.base`](https://svelte.dev/docs/kit/configuration#paths).
*
Expand All @@ -3146,10 +3146,10 @@ declare module '$app/paths' {
/**
* @deprecated Use [`resolve(...)`](https://svelte.dev/docs/kit/$app-paths#resolve) instead
*/
export function resolveRoute<T extends RouteId | Pathname>(
export function resolveRoute<T extends RouteId | ResolvablePath>(
...args: ResolveArgs<T>
): ResolvedPathname;
type ResolveArgs<T extends RouteId | Pathname> = T extends RouteId
type ResolveArgs<T extends RouteId | ResolvablePath> = T extends RouteId
? RouteParams<T> extends Record<string, never>
? [route: T]
: [route: T, params: RouteParams<T>]
Expand Down Expand Up @@ -3191,7 +3191,7 @@ declare module '$app/paths' {
* @since 2.26
*
* */
export function resolve<T extends RouteId | Pathname>(...args: ResolveArgs<T>): ResolvedPathname;
export function resolve<T extends RouteId | ResolvablePath>(...args: ResolveArgs<T>): ResolvedPathname;
/**
* Match a path or URL to a route ID and extracts any parameters.
*
Expand Down Expand Up @@ -3596,6 +3596,16 @@ declare module '$app/types' {
*/
export type Pathname = ReturnType<AppTypes['Pathname']>;

/**
* A pathname that may optionally include a query string and/or hash fragment.
* Used as input to the `resolve` function.
*/
export type ResolvablePath =
| Pathname
| `${Pathname}?${string}`
| `${Pathname}#${string}`
| `${Pathname}?${string}#${string}`;

/**
* `Pathname`, but possibly prefixed with a base path. Used for `page.url.pathname`.
*/
Expand Down
Loading