Skip to content

feat: typed params prop for page/layout components #13999

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

Merged
merged 1 commit into from
Jul 15, 2025
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/long-bars-rush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': minor
---

feat: typed `params` prop for page/layout components
12 changes: 6 additions & 6 deletions packages/kit/src/core/sync/write_root.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,25 @@ export function write_root(manifest_data, output) {
isSvelte5Plus()
? dedent`{@const Pyramid_${l} = constructors[${l}]}
<!-- svelte-ignore binding_property_non_reactive -->
<Pyramid_${l} bind:this={components[${l}]} data={data_${l}} {form}>
<Pyramid_${l} bind:this={components[${l}]} data={data_${l}} {form} params={page.params}>
${pyramid}
</Pyramid_${l}>`
: dedent`<svelte:component this={constructors[${l}]} bind:this={components[${l}]} data={data_${l}}>
: dedent`<svelte:component this={constructors[${l}]} bind:this={components[${l}]} data={data_${l}} params={page.params}>
${pyramid}
</svelte:component>`
}

{:else}
${
isSvelte5Plus()
? dedent`
{@const Pyramid_${l} = constructors[${l}]}
<!-- svelte-ignore binding_property_non_reactive -->
<Pyramid_${l} bind:this={components[${l}]} data={data_${l}} {form} />
<Pyramid_${l} bind:this={components[${l}]} data={data_${l}} {form} params={page.params} />
`
: dedent`<svelte:component this={constructors[${l}]} bind:this={components[${l}]} data={data_${l}} {form} />`
: dedent`<svelte:component this={constructors[${l}]} bind:this={components[${l}]} data={data_${l}} {form} params={page.params} />`
}

{/if}
`;
}
Expand Down
8 changes: 5 additions & 3 deletions packages/kit/src/core/sync/write_types/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,11 @@ function update_types(config, routes, route, to_delete = new Set()) {
}

if (route.leaf.server) {
exports.push('export type PageProps = { data: PageData; form: ActionData }');
exports.push(
'export type PageProps = { params: RouteParams; data: PageData; form: ActionData }'
);
} else {
exports.push('export type PageProps = { data: PageData }');
exports.push('export type PageProps = { params: RouteParams; data: PageData }');
}
}

Expand Down Expand Up @@ -341,7 +343,7 @@ function update_types(config, routes, route, to_delete = new Set()) {
if (proxies.universal?.modified) to_delete.delete(proxies.universal.file_name);

exports.push(
'export type LayoutProps = { data: LayoutData; children: import("svelte").Snippet }'
'export type LayoutProps = { params: LayoutParams; data: LayoutData; children: import("svelte").Snippet }'
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<script>
let { children } = $props();
</script>

<a href="/params-prop/123">123</a>
<a href="/params-prop/456">456</a>

{@render children()}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
let { params } = $props();
</script>

<p>x: {params.x}</p>
12 changes: 12 additions & 0 deletions packages/kit/test/apps/basics/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1556,3 +1556,15 @@ test.describe('getRequestEvent', () => {
expect(await page.textContent('h1')).toBe('Crashing now (500 hello from hooks.server.js)');
});
});

test.describe('params prop', () => {
test('params prop is passed to the page', async ({ page, clicknav }) => {
await page.goto('/params-prop');

await clicknav('[href="/params-prop/123"]');
await expect(page.locator('p')).toHaveText('x: 123');

await clicknav('[href="/params-prop/456"]');
await expect(page.locator('p')).toHaveText('x: 456');
});
});
Loading