Skip to content

Commit

Permalink
feat: Auto-typing support for entries (#1983)
Browse files Browse the repository at this point in the history
  • Loading branch information
tcc-sejohnson committed Apr 28, 2023
1 parent d5b29f0 commit 651db67
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions packages/svelte2tsx/src/helpers/sveltekit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,23 @@ function upserKitRouteFile(
insert(pos, inserted);
}

// add type to entries function if not explicitly typed
const entries = exports.get('entries');
if (
entries?.type === 'function' &&
entries.node.parameters.length === 0 &&
!entries.hasTypeDefinition &&
!basename.includes('layout')
) {
if (!entries.node.type && entries.node.body) {
const returnPos = ts.isArrowFunction(entries.node)
? entries.node.equalsGreaterThanToken.getStart()
: entries.node.body.getStart();
const returnInsertion = surround(`: ReturnType<import('./$types.js').EntryGenerator> `);
insert(returnPos, returnInsertion);
}
}

// add type to actions variable if not explicitly typed
const actions = exports.get('actions');
if (actions?.type === 'var' && !actions.hasTypeDefinition && actions.node.initializer) {
Expand Down

0 comments on commit 651db67

Please sign in to comment.