Closed Serator closed 1 week ago
By separating the properties, the relationship provided by the type is broken. If you don't split, it should work as intended:
<script lang="ts">
import Button from './Button.svelte'
import type { ComponentProps } from 'svelte'
let { ...props }: ComponentProps<Button> = $props()
</script>
<Button {...props} class="bg-red-400" />
Hmmm, this is not the pattern I use to describe properties as I prefer a more explicit way, but it works. Thanks! šš»
Could you please clarify for this example from the code above?
<Button {...false ? {a, b} : {}} class="bg-red-400" />
Here I explicitly pass "nothing", but TS still outputs an error.
It's more about TS itself. When you destruct a
and b
, their types get squashed. Example.
Oh, it all makes sense now. Thanks! šš»
Describe the bug
Hi there. I am trying to create a component on Svelte 5 with conditional types, where property
a
is only possible if it has propertyb
.+page.svelte
:RedButton.svelte
:Button.svelte
:How can I pass properties to
RedButton.svelte
typing them withComponentProps<Button>
and not get type errors? Thanks!Reproduction
https://stackblitz.com/edit/vitejs-vite-fzujoz?file=src%2Froutes%2FRedButton.svelte
Logs
System Info
Severity
annoyance