Skip to content

Commit

Permalink
Add example for failing TypeScript case with required snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
xeho91 committed May 25, 2024
1 parent 4349e20 commit 3dd8d62
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
29 changes: 29 additions & 0 deletions stories/RequiredSnippet.stories.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<script context="module" lang="ts">
import { defineMeta } from '@storybook/addon-svelte-csf';
import RequiredSnippet from './RequiredSnippet.svelte';
const { Story } = defineMeta({
component: RequiredSnippet,
tags: ['autodocs'],
});
</script>


<!-- Not working, as expected -->
<!-- <Story name="Case 1" /> -->

{#snippet children()}
<p>This works</p>
{/snippet}

<!-- Works, as expected -->
<Story name="Case 2" args={{ children }} />

<!-- Works, as expected, but TypeScript is not happy -->
<Story name="Case 3">
<!-- Current workaround: add `args: { children: "" }` to `defineMeta`, but this is invalid, Svelte snippets cannot be literal, they're functions -->
{#snippet children()}
<p>This works</p>
{/snippet}
</Story>
13 changes: 13 additions & 0 deletions stories/RequiredSnippet.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<script lang="ts">
import type { Snippet } from "svelte";
interface Props {
children: Snippet;
}
let { children }: Props = $props();
</script>

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

0 comments on commit 3dd8d62

Please sign in to comment.