Skip to content

fix: ignore route groups when generating typed routes #14050

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 5 commits into from
Jul 25, 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/tidy-owls-enter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: ignore route groups when generating typed routes
17 changes: 12 additions & 5 deletions packages/kit/src/core/sync/write_types/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@ import { posixify, rimraf, walk } from '../../../utils/filesystem.js';
import { compact } from '../../../utils/array.js';
import { ts } from '../ts.js';
import { s } from '../../../utils/misc.js';
import { get_route_segments } from '../../../utils/routing.js';

const remove_relative_parent_traversals = (/** @type {string} */ path) =>
path.replace(/\.\.\//g, '');
const replace_optional_params = (/** @type {string} */ id) =>
id.replace(/\/\[\[[^\]]+\]\]/g, '${string}');
const replace_required_params = (/** @type {string} */ id) =>
id.replace(/\/\[[^\]]+\]/g, '/${string}');
/** Convert route ID to pathname by removing layout groups */
const remove_group_segments = (/** @type {string} */ id) => {
return '/' + get_route_segments(id).join('/');
};
const is_whitespace = (/** @type {string} */ char) => /\s/.test(char);

/**
Expand Down Expand Up @@ -60,8 +65,8 @@ export function write_all_types(config, manifest_data) {
}
}

/** @type {string[]} */
const pathnames = [];
/** @type {Set<string>} */
const pathnames = new Set();

/** @type {string[]} */
const dynamic_routes = [];
Expand All @@ -76,9 +81,11 @@ export function write_all_types(config, manifest_data) {

dynamic_routes.push(route_type);

pathnames.push(`\`${replace_required_params(replace_optional_params(route.id))}\` & {}`);
const pathname = remove_group_segments(route.id);
pathnames.add(`\`${replace_required_params(replace_optional_params(pathname))}\` & {}`);
} else {
pathnames.push(s(route.id));
const pathname = remove_group_segments(route.id);
pathnames.add(s(pathname));
}

/** @type {Map<string, boolean>} */
Expand Down Expand Up @@ -113,7 +120,7 @@ export function write_all_types(config, manifest_data) {
`export type RouteId = ${manifest_data.routes.map((r) => s(r.id)).join(' | ')};`,
'export type RouteParams<T extends RouteId> = T extends keyof DynamicRoutes ? DynamicRoutes[T] : Record<string, never>;',
'export type LayoutParams<T extends RouteId> = Layouts[T] | Record<string, never>;',
`export type Pathname = ${pathnames.join(' | ')};`,
`export type Pathname = ${Array.from(pathnames).join(' | ')};`,
'export type ResolvedPathname = `${"" | `/${string}`}${Pathname}`;',
`export type Asset = ${manifest_data.assets.map((asset) => s('/' + asset.file)).join(' | ') || 'never'};`
].join('\n\n')
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/core/sync/write_types/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function run_test(dir) {
write_all_types(initial, manifest);
}

test('Creates correct $types', { timeout: 6000 }, () => {
test('Creates correct $types', { timeout: 10000 }, () => {
// To save us from creating a real SvelteKit project for each of the tests,
// we first run the type generation directly for each test case, and then
// call `tsc` to check that the generated types are valid.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ declare let id: RouteId;
// okay
id = '/';
id = '/foo/[bar]/[baz]';
id = '/(group)/path-a';

// @ts-expect-error
id = '/nope';
Expand All @@ -26,5 +27,10 @@ pathname = '/nope';
pathname = '/foo';
pathname = '/foo/1/2';

// Test layout groups
pathname = '/path-a';
// @ts-expect-error layout group names are NOT part of the pathname type
pathname = '/(group)/path-a';

// read `pathname` otherwise it is treated as unused
pathname;
2 changes: 1 addition & 1 deletion packages/kit/src/utils/routing.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export function remove_optional_params(id) {
* @param {string} segment
*/
function affects_path(segment) {
return !/^\([^)]+\)$/.test(segment);
return segment !== '' && !/^\([^)]+\)$/.test(segment);
}

/**
Expand Down