Skip to content

Commit b352f08

Browse files
fix: migrating rest props type includes props types (#13632)
* fix: rest props type includes props types * chore: remove extra line * Update .changeset/spotty-kings-hug.md
1 parent d7cf76b commit b352f08

File tree

9 files changed

+40
-20
lines changed

9 files changed

+40
-20
lines changed

.changeset/spotty-kings-hug.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: migrating rest props type includes props types

packages/svelte/src/compiler/migrate/index.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -198,26 +198,26 @@ export function migrate(source, { filename } = {}) {
198198
const type_name = state.scope.root.unique('Props').name;
199199
let type = '';
200200
if (uses_ts) {
201+
type = `interface ${type_name} {${newline_separator}${state.props
202+
.map((prop) => {
203+
const comment = prop.comment ? `${prop.comment}${newline_separator}` : '';
204+
return `${comment}${prop.exported}${prop.optional ? '?' : ''}: ${prop.type};`;
205+
})
206+
.join(newline_separator)}`;
201207
if (analysis.uses_props || analysis.uses_rest_props) {
202-
type = `interface ${type_name} { [key: string]: any }`;
203-
} else {
204-
type = `interface ${type_name} {${newline_separator}${state.props
205-
.map((prop) => {
206-
const comment = prop.comment ? `${prop.comment}${newline_separator}` : '';
207-
return `${comment}${prop.exported}${prop.optional ? '?' : ''}: ${prop.type};`;
208-
})
209-
.join(newline_separator)}\n${indent}}`;
208+
type += `${state.props.length > 0 ? newline_separator : ''}[key: string]: any`;
210209
}
210+
type += `\n${indent}}`;
211211
} else {
212+
type = `{${state.props
213+
.map((prop) => {
214+
return `${prop.exported}${prop.optional ? '?' : ''}: ${prop.type}`;
215+
})
216+
.join(`, `)}`;
212217
if (analysis.uses_props || analysis.uses_rest_props) {
213-
type = `Record<string, any>`;
214-
} else {
215-
type = `{${state.props
216-
.map((prop) => {
217-
return `${prop.exported}${prop.optional ? '?' : ''}: ${prop.type}`;
218-
})
219-
.join(`, `)}}`;
218+
type += `${state.props.length > 0 ? ', ' : ''}[key: string]: any`;
220219
}
220+
type += '}';
221221
}
222222

223223
let props_declaration = `let {${props_separator}${props}${has_many_props ? `\n${indent}` : ' '}}`;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script lang="ts">
2+
export let foo: string;
3+
</script>
4+
5+
<button {foo} {...$$restProps}>click me</button>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<script lang="ts">
2+
interface Props {
3+
foo: string;
4+
[key: string]: any
5+
}
6+
7+
let { foo, ...rest }: Props = $props();
8+
</script>
9+
10+
<button {foo} {...rest}>click me</button>

packages/svelte/tests/migrate/samples/props-rest-props/output.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script>
2-
/** @type {Record<string, any>} */
2+
/** @type {{foo: any, [key: string]: any}} */
33
let { foo, ...rest } = $props();
44
</script>
55

packages/svelte/tests/migrate/samples/slots-with-$$props/output.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script>
2-
/** @type {Record<string, any>} */
2+
/** @type {{children?: import('svelte').Snippet, foo_1?: import('svelte').Snippet<[any]>, bar?: import('svelte').Snippet, dashed_name?: import('svelte').Snippet, [key: string]: any}} */
33
let {
44
...props
55
} = $props();

packages/svelte/tests/migrate/samples/svelte-component/output.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script>
2-
/** @type {Record<string, any>} */
2+
/** @type {{[key: string]: any}} */
33
let { ...rest } = $props();
44
let Component;
55
let fallback;

packages/svelte/tests/migrate/samples/svelte-self-name-conflict/output.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script>
22
import Output_1 from './output.svelte';
3-
/** @type {Record<string, any>} */
3+
/** @type {{[key: string]: any}} */
44
let { ...props } = $props();
55
let Output;
66
</script>

packages/svelte/tests/migrate/samples/svelte-self/output.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script>
22
import Output from './output.svelte';
3-
/** @type {Record<string, any>} */
3+
/** @type {{[key: string]: any}} */
44
let { ...props } = $props();
55
</script>
66

0 commit comments

Comments
 (0)