What's the best practise to type snippets automatically? #14521
Answered
by
brunnerh
CNSeniorious000
asked this question in
Q&A
-
In Svelte 4, if we inject something to a For example, if I have a <script lang="ts">
async function copy(content: string) {
await navigator.clipboard.writeText(content)
toast("Successfully copied!")
}
</script>
<slot {copy} /> In Svelte 5, it seems I have to manually claim its type like this: <script lang="ts">
import type { Snippet } from "svelte";
async function copy(content: string) {
await navigator.clipboard.writeText(content)
toast("Successfully copied!")
}
const { children }: { children: Snippet<[{ copy: typeof copy }]> } = $props()
</script>
{@render children({ copy })} BTW, the markup where I uses it became much lines longer too
From <UseCopy let:copy>
<button on:click={() => copy(content)} />
</UseCopy> To <UseCopy>
{#snippet children({ copy })}
<button on:click={() => copy(content)}></button>
{/snippet}
</UseCopy> How to avoid this? Are there some best practises for these usages? |
Beta Was this translation helpful? Give feedback.
Answered by
brunnerh
Dec 3, 2024
Replies: 1 comment 4 replies
-
To my knowledge there is currently no way around this if you use snippet arguments. |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Well, the language tools could potentially generate types based on usage, so you could e.g. have something like: