Skip to content

Commit b8bd5c6

Browse files
authored
fix type generation for matched params (#4472)
* fix type generation for matched params * Update .changeset/serious-ducks-exercise.md
1 parent e962c82 commit b8bd5c6

File tree

2 files changed

+8
-17
lines changed

2 files changed

+8
-17
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
Generate correct types for routes with parameter matchers

packages/kit/src/core/sync/write_types.js

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { parse_route_id } from '../../utils/routing.js';
12
import { write_if_changed } from './utils.js';
23

34
/** @param {string} imports */
@@ -24,21 +25,6 @@ export function write_types(config, manifest_data) {
2425
/** @type {Map<string, { params: string[], type: 'page' | 'endpoint' | 'both' }>} */
2526
const shadow_types = new Map();
2627

27-
/** @param {string} key */
28-
function extract_params(key) {
29-
/** @type {string[]} */
30-
const params = [];
31-
32-
const pattern = /\[(?:\.{3})?([^\]]+)\]/g;
33-
let match;
34-
35-
while ((match = pattern.exec(key))) {
36-
params.push(match[1]);
37-
}
38-
39-
return params;
40-
}
41-
4228
manifest_data.routes.forEach((route) => {
4329
const file = route.type === 'endpoint' ? route.file : route.shadow;
4430

@@ -48,7 +34,7 @@ export function write_types(config, manifest_data) {
4834
);
4935
const key = file.slice(0, -ext.length);
5036
shadow_types.set(key, {
51-
params: extract_params(key),
37+
params: parse_route_id(key).names,
5238
type: route.type === 'endpoint' ? 'endpoint' : 'both'
5339
});
5440
}
@@ -61,7 +47,7 @@ export function write_types(config, manifest_data) {
6147
const key = component.slice(0, -ext.length);
6248

6349
if (!shadow_types.has(key)) {
64-
shadow_types.set(key, { params: extract_params(key), type: 'page' });
50+
shadow_types.set(key, { params: parse_route_id(key).names, type: 'page' });
6551
}
6652
});
6753

0 commit comments

Comments
 (0)