Skip to content
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/calm-types-bind.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: detect destructured `load` and `actions` exports during type generation
14 changes: 11 additions & 3 deletions packages/kit/src/core/sync/write_types/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,16 @@ export function tweak_types(content, is_server) {
const code = new MagicString(content);

const exports = new Map();
/** @param {import('typescript').BindingName} name */
function add_export(name) {
if (ts.isIdentifier(name)) {
if (names.has(name.text)) exports.set(name.text, name.text);
} else {
for (const element of name.elements) {
if (ts.isBindingElement(element)) add_export(element.name);
}
}
}

ast.forEachChild((node) => {
if (
Expand All @@ -662,9 +672,7 @@ export function tweak_types(content, is_server) {

if (ts.isVariableStatement(node)) {
node.declarationList.declarations.forEach((declaration) => {
if (ts.isIdentifier(declaration.name) && names.has(declaration.name.text)) {
exports.set(declaration.name.text, declaration.name.text);
}
add_export(declaration.name);
});
}
}
Expand Down
10 changes: 10 additions & 0 deletions packages/kit/src/core/sync/write_types/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,16 @@ test('Rewrites types for a JavaScript module with `const`', () => {
);
});

test('Detects destructured exports', () => {
const source = `
export const { load, actions } = create_page();
`;

const rewritten = tweak_types(source, true);

expect(rewritten?.exports).toEqual(['load', 'actions']);
});

test('Appends @ts-nocheck after @ts-check', () => {
const source = `// @ts-check
/** @type {import('./$types').Get} */
Expand Down
Loading