Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/easy-pianos-float.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': major
---

breaking: make `page.url` immutable on a type level
10 changes: 9 additions & 1 deletion packages/kit/src/exports/public.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1454,7 +1454,7 @@ export interface Page<
/**
* The URL of the current page.
*/
url: URL & { pathname: ResolvedPathname };
url: ReadonlyURL & { readonly pathname: ResolvedPathname | (string & {}) };
/**
* The parameters of the current page - e.g. for a route like `/blog/[slug]`, a `{ slug: string }` object.
*/
Expand Down Expand Up @@ -1995,6 +1995,14 @@ export interface Snapshot<T = any> {
restore: (snapshot: T) => void;
}

export type ReadonlyURLSearchParams = Omit<URLSearchParams, 'set' | 'append' | 'delete' | 'sort'>;
Comment thread
dummdidumm marked this conversation as resolved.

export type ReadonlyURL = Readonly<
Omit<URL, 'searchParams'> & {
searchParams: ReadonlyURLSearchParams;
}
>;

// If T is unknown or has an index signature, the types below will recurse indefinitely and create giant unions that TS can't handle
type WillRecurseIndefinitely<T> = unknown extends T ? true : string extends keyof T ? true : false;

Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -2649,7 +2649,7 @@ export async function applyAction(result) {
root.$set({ form: result.data });

if (result.type === 'success') {
reset_focus(page.url);
reset_focus(/** @type {URL} */ (page.url));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
let { data } = $props();

function update_q() {
// @ts-expect-error set is not in the types; we wanna test here that we guard against mutation in goto, too
page.url.searchParams.set('q', 'updated');
// @ts-expect-error
goto(page.url);
}
</script>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** @typedef {Record<string, string[]>} Query */

/** @param {URLSearchParams} query */
/** @param {import("@sveltejs/kit").ReadonlyURLSearchParams} query */
export function to_pojo(query) {
/** @type {Query}*/
const values = {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
<button
type="button"
onclick={() => {
// @ts-expect-error set is not in the types; we wanna test here that we guard against mutation in goto, too
page.url.searchParams.set('q', 'test');
// @ts-expect-error
goto(page.url);
}}>test</button
>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** @typedef {Record<string, string[]>} Query */

/** @param {URLSearchParams} query */
/** @param {import("@sveltejs/kit").ReadonlyURLSearchParams} query */
export function to_pojo(query) {
/** @type {Query}*/
const values = {};
Expand Down
10 changes: 9 additions & 1 deletion packages/kit/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1427,7 +1427,7 @@ declare module '@sveltejs/kit' {
/**
* The URL of the current page.
*/
url: URL & { pathname: ResolvedPathname };
url: ReadonlyURL & { readonly pathname: ResolvedPathname | (string & {}) };
/**
* The parameters of the current page - e.g. for a route like `/blog/[slug]`, a `{ slug: string }` object.
*/
Expand Down Expand Up @@ -1966,6 +1966,14 @@ declare module '@sveltejs/kit' {
restore: (snapshot: T) => void;
}

export type ReadonlyURLSearchParams = Omit<URLSearchParams, 'set' | 'append' | 'delete' | 'sort'>;

export type ReadonlyURL = Readonly<
Omit<URL, 'searchParams'> & {
searchParams: ReadonlyURLSearchParams;
}
>;

// If T is unknown or has an index signature, the types below will recurse indefinitely and create giant unions that TS can't handle
type WillRecurseIndefinitely<T> = unknown extends T ? true : string extends keyof T ? true : false;

Expand Down