What is the correct TypeScript type for the `children` property in Svelte 5? - Stack Overflow

时间: 2025-01-06 admin 业界

In Svelte 5 it now retrieves children from the the $props() rune. I wasn't able to find any documentation stating how the props reserved children property should be typed and the type is not included in the $props() rune by default.

<script lang="ts">
    import type { Snippet } from 'svelte';

    interface Props {
        children: ?????; // What do I type this as?
    }
    const { children }: Props = $props();
</script>

<div>
    {@render children?.()}
</div>

In Svelte 5 it now retrieves children from the the $props() rune. I wasn't able to find any documentation stating how the props reserved children property should be typed and the type is not included in the $props() rune by default.

<script lang="ts">
    import type { Snippet } from 'svelte';

    interface Props {
        children: ?????; // What do I type this as?
    }
    const { children }: Props = $props();
</script>

<div>
    {@render children?.()}
</div>
Share Improve this question asked 15 hours ago Daniel TononDaniel Tonon 10.2k5 gold badges65 silver badges67 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

children is typed as Snippet (imported from 'svelte')

<script lang="ts">
    import type { Snippet } from 'svelte';

    interface Props {
        children: Snippet;
    }
    const { children }: Props = $props();
</script>

<div>
    {@render children?.()}
</div>
最新文章