Skip to content

Commit 651db67

Browse files
feat: Auto-typing support for entries (#1983)
See sveltejs/kit#9571
1 parent d5b29f0 commit 651db67

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

packages/svelte2tsx/src/helpers/sveltekit.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,23 @@ function upserKitRouteFile(
171171
insert(pos, inserted);
172172
}
173173

174+
// add type to entries function if not explicitly typed
175+
const entries = exports.get('entries');
176+
if (
177+
entries?.type === 'function' &&
178+
entries.node.parameters.length === 0 &&
179+
!entries.hasTypeDefinition &&
180+
!basename.includes('layout')
181+
) {
182+
if (!entries.node.type && entries.node.body) {
183+
const returnPos = ts.isArrowFunction(entries.node)
184+
? entries.node.equalsGreaterThanToken.getStart()
185+
: entries.node.body.getStart();
186+
const returnInsertion = surround(`: ReturnType<import('./$types.js').EntryGenerator> `);
187+
insert(returnPos, returnInsertion);
188+
}
189+
}
190+
174191
// add type to actions variable if not explicitly typed
175192
const actions = exports.get('actions');
176193
if (actions?.type === 'var' && !actions.hasTypeDefinition && actions.node.initializer) {

0 commit comments

Comments
 (0)