Search code examples
sveltesvelte-5

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


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">
    interface Props {
        children: ?????; // What do I type this as?
    }
    const { children }: Props = $props();
</script>

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

Solution

  • 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>