-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Destructuring of discriminated unions in the presence of bindable props in Svelte 5 #15675
Comments
You can typecast binding in the template. Not the best solution, but it works with both Svelte and TS. <script lang="ts">
type Props = ...;
let { userInputType, value = $bindable() }: Props = $props();
if (userInputType === "float") {
const a = value as number;
console.log(a);
}
</script>
{#if userInputType === "boolean"}
<input type="checkbox" bind:checked={value as boolean} />
{:else if userInputType === "string"}
<input class="input" type="text" bind:value={value} />
{:else if userInputType === "int"}
<input class="input" type="number" step="1" bind:value={value} />
{:else if userInputType === "float"}
<input class="input" type="number" bind:value={value} />
{:else}
<span></span>
{/if}
|
Thanks for the quick response!
This causes an error in my IDE, so I assumed that it will error at runtime, too. However, it seems that svelte-check is OK with it. It seems that the Svelte plugin for IDEA is not completely up to speed with Svelte 5 yet: https://plugins.jetbrains.com/plugin/12375-svelte/reviews
Sure, I should have chosen a better example: As soon as you want to extract that input to your own component, then it won't be In summary, the issue stands, but I have been helped with a more succinct workaround (i.e., using |
The only real solution apart from type gymnastics is either TS gets much smarter about how types relate to each other (AFAIK it already is good at this when you use |
This is what I'm lobbying for :)
Why do you consider my proposal to be a separate API? I understand that |
Describe the problem
Context
At #9241 (comment), @Rich-Harris wrote:
Now this is exactly the type of thing I'm trying to do, but it's not working as advertised, due to some quirks of Svelte and Typescript not loving each other:
Example 1: No destructuring, not bindable.
Type narrowing works fine, but
value
is not bindable:Using the component causes an error:
Example 2: Bindable, no destructuring.
If I follow the advice from the error message above, and the documentation of
$bindable
, the type narrowing stops working. See the error messages in the comments:Example 3: Having both a destructured and a non-destructured version of the props is not possible
Example 4: Restructuring the destructured props is hacky and breaks reactivity
Workaround 1: Use type
any
:(This removes a lot of type safety:
Workaround 2: Use custom two-way reactivity
I'm not even sure if this is correct:
In Vue, I would have used a
WritableComputed
for this:Describe the proposed solution
Possible solution 1
Allow
$bindable()
to be used like this:This currently causes:
Possible solution 2
as any
) inside the template.@ts-expect-error
and@ts-ignore
inside the template.Possible solution 3
I could not think of any others yet.
Importance
would make my life easier
The text was updated successfully, but these errors were encountered: