Skip to content

Commit 4264a36

Browse files
authored
fix: svelte 5 prop types (#14934)
1 parent ef53716 commit 4264a36

File tree

14 files changed

+131
-7
lines changed

14 files changed

+131
-7
lines changed

.changeset/olive-lands-try.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@astrojs/svelte': patch
3+
---
4+
5+
Fixes an issue where Svelte 5 components used in Astro files would not have proper type checking and IntelliSense.

packages/integrations/svelte/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@
2424
"./editor": "./dist/editor.cjs",
2525
"./client.js": "./dist/client.svelte.js",
2626
"./server.js": "./dist/server.js",
27+
"./svelte-shims.d.ts": "./svelte-shims.d.ts",
2728
"./package.json": "./package.json"
2829
},
2930
"files": [
30-
"dist"
31+
"dist",
32+
"svelte-shims.d.ts"
3133
],
3234
"scripts": {
3335
"build": "astro-scripts build \"src/**/*.ts\" && astro-scripts build \"src/editor.cts\" --force-cjs --no-clean-dist && tsc",

packages/integrations/svelte/src/editor.cts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,22 @@ export function toTSX(code: string, className: string): string {
77
`;
88

99
try {
10-
let tsx = svelte2tsx(code, { mode: 'ts' }).code;
11-
tsx = '/// <reference types="svelte2tsx/svelte-shims" />\n' + tsx;
12-
result = tsx.replace(
13-
'export default class extends __sveltets_2_createSvelte2TsxComponent(',
14-
`export default function ${className}__AstroComponent_(_props: typeof Component.props): any {}\nlet Component = `,
15-
);
10+
let tsx = svelte2tsx(code, { mode: 'ts', isTsFile: true }).code;
11+
tsx = "import '@astrojs/svelte/svelte-shims.d.ts';\n" + tsx;
12+
13+
// New svelte2tsx output (Svelte 5)
14+
if (tsx.includes('export default $$Component;')) {
15+
result = tsx.replace(
16+
'export default $$Component;',
17+
`export default function ${className}__AstroComponent_(_props: import('svelte').ComponentProps<typeof $$$$Component>): any {}`,
18+
);
19+
} else {
20+
// Old svelte2tsx output
21+
result = tsx.replace(
22+
'export default class extends __sveltets_2_createSvelte2TsxComponent(',
23+
`export default function ${className}__AstroComponent_(_props: typeof Component.props): any {}\nlet Component = `,
24+
);
25+
}
1626
} catch {
1727
return result;
1828
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import 'svelte2tsx/svelte-shims-v4';
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import assert from 'node:assert/strict';
2+
import { describe, it } from 'node:test';
3+
import { fileURLToPath } from 'node:url';
4+
import { cli } from '../../../astro/test/test-utils.js';
5+
6+
describe('Svelte Check', () => {
7+
it('should fail check on type error', async () => {
8+
const root = fileURLToPath(new URL('./fixtures/prop-types/', import.meta.url));
9+
const { getResult } = cli('check', '--root', root);
10+
const { exitCode, stdout } = await getResult();
11+
12+
assert.equal(exitCode, 1, 'Expected check to fail (exit code 1)');
13+
assert.ok(
14+
stdout.includes(`Type 'string' is not assignable to type 'number'`),
15+
'Expected specific type error message',
16+
);
17+
});
18+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<script lang="ts">
2+
interface Props {
3+
name: string;
4+
age?: number;
5+
bold?: boolean;
6+
}
7+
8+
let { name, age = 0, bold = false }: Props = $props();
9+
</script>
10+
11+
<p class:bold={bold}>
12+
Hello {name}, you are {age} years old.
13+
</p>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"extends": "astro/tsconfigs/strict",
3+
"include": [".astro/types.d.ts", "**/*"],
4+
"exclude": ["dist"]
5+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { defineConfig } from 'astro/config';
2+
import svelte from '@astrojs/svelte';
3+
4+
export default defineConfig({
5+
srcDir: './types',
6+
integrations: [svelte()]
7+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "svelte-prop-types",
3+
"type": "module",
4+
"version": "0.0.1",
5+
"scripts": {
6+
"dev": "astro dev",
7+
"build": "astro build",
8+
"preview": "astro preview",
9+
"astro": "astro"
10+
},
11+
"dependencies": {
12+
"@astrojs/svelte": "^7.2.2",
13+
"astro": "^5.16.0",
14+
"svelte": "^5.43.14"
15+
}
16+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default {};

0 commit comments

Comments
 (0)