Skip to content

Commit

Permalink
docs: SvelteHTMLElements can be used for creating component wrapper (
Browse files Browse the repository at this point in the history
  • Loading branch information
sacrosanctic authored Nov 6, 2024
1 parent 9b2a8f1 commit 26d109c
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion documentation/docs/07-misc/03-typescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ The content of `generics` is what you would put between the `<...>` tags of a ge

## Typing wrapper components

In case you're writing a component that wraps a native element, you may want to expose all the attributes of underlying element to the user. In that case, use (or extend from) one of the interfaces provided by `svelte/elements`. Here's an example for a `Button` component:
In case you're writing a component that wraps a native element, you may want to expose all the attributes of the underlying element to the user. In that case, use (or extend from) one of the interfaces provided by `svelte/elements`. Here's an example for a `Button` component:

```svelte
<script lang="ts">
Expand All @@ -146,6 +146,20 @@ In case you're writing a component that wraps a native element, you may want to
</button>
```

Not all elements have a specific type definition, in those cases, use `SvelteHTMLElements`:

```svelte
<script lang="ts">
import type { SvelteHTMLElements } from 'svelte/elements';
let { children, ...rest }: SvelteHTMLElements['div'] = $props();
</script>
<div {...rest}>
{@render children()}
</div>
```

## Typing `$state`

You can type `$state` like any other variable.
Expand Down

0 comments on commit 26d109c

Please sign in to comment.