Svelte: Feature request: group unknown props for forwarding

Created on 2 Jun 2019  Â·  1Comment  Â·  Source: sveltejs/svelte

All of the props that are passed to a component are available through $$props, but there isn't a great way to access the "unknown" (non-exported) props. Having the ability to access these props would make it more convenient to forward attributes down to a native DOM element.

Currently, my workaround is to destructure $$props with every known prop name.

<div {...rest} id={computed}>
  <slot></slot>
</div>

<script>
  export let one;
  export let two;

  $: computed = `${one}-${two}`;

  const {
    one: _one,
    two: _two,
    '$$slots': _slots,
    '$$scope': _scope,
    ...rest
  } = $$props;

</script>

```html

Testing
My ideal use would look more like:

```html
<div {...$$attrs} id={computed}>
  <slot></slot>
</div>

<script>
  export let one;
  export let two;

  $: computed = `${one}-${two}`;
</script>

I used $$attrs since Vue has an $attrs property (based on its inheritedAttrs), but I'd happily type $$theseAreTheUnexportedProps if it saved me from doing the destructuring myself.

If this isn't ideal default behavior, maybe it could be controlled with an option?

<svelte:options inherit={true} />
enhancement proposal

Most helpful comment

In 3.20.0, you now have access to $$restProps which is an object of all props _not_ explicitly declared as exports in the component! 🎉

>All comments

In 3.20.0, you now have access to $$restProps which is an object of all props _not_ explicitly declared as exports in the component! 🎉

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sskyy picture sskyy  Â·  3Comments

clitetailor picture clitetailor  Â·  3Comments

bestguy picture bestguy  Â·  3Comments

thoughtspile picture thoughtspile  Â·  3Comments

mmjmanders picture mmjmanders  Â·  3Comments