Skip to content
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

Allow declaration of $bindable fields on the union type prop. #15209

Open
Aron-Lomner opened this issue Feb 4, 2025 · 2 comments
Open

Allow declaration of $bindable fields on the union type prop. #15209

Aron-Lomner opened this issue Feb 4, 2025 · 2 comments

Comments

@Aron-Lomner
Copy link

Describe the problem

Problem:
It is not possible to have a $bindable field on the union portion of a Prop.

Explanation:
Svelte only allows declaring a prop field as $bindable from within a de-structuring.

  let { lowerValue = $bindable(), upperValue = $bindable(), value = $bindable() }: Props = $props();

This works fine for simple types, but if you had a union type like so:

type Props =
    | {
        lowerValue: number;
        upperValue: number;
      }
    | { value: number };

Then you can not de-structure.

Describe the proposed solution

Solution:
Add an interface for declaring $bindables without de-structuring.
For Example:

type Props =
   | {
       lowerValue: $bindable(number) ;
       upperValue: $bindable(number);
     }
   | { value: $bindable(number) };

Or:

type Props =
    | {
        lowerValue: number ;
        upperValue: number;
      }
    | { value: number };
  let props: Props = $props();
  props.lowerValue = $bindable()

Importance

would make my life easier

@7nik
Copy link
Contributor

7nik commented Feb 4, 2025

Shouldn't it then be two components? Maybe one wrapping another, or with common logic in "private" .ts file, or something.

@7nik
Copy link
Contributor

7nik commented Feb 4, 2025

And, to clarify, the problem is only with Typescript.
You can do

	type Props =
    | {
        lowerValue: string;
        upperValue: string;
        value?: never;
      }
    | {
        value: string
        lowerValue?: never;
        upperValue?: never;
     };

Which will give you the desired result. Not perfect, but totally working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants