Skip to content

Nested Destructuring of $props() #14700

Open
@Jazza-231

Description

@Jazza-231

Describe the problem

I often find myself destructuring $props. In cases such as

const { index } = $props()

This is, as shown, simple and clean. But what if the prop we are destructuring is in itself an object?

const { suggestion: { id } } = $props()

The above does not work, and gives the following error:

$props() assignment must not contain nested properties or computed keys
https://svelte.dev/e/props_invalid_pattern

Instead, we have to destructure in another line, like so:

const { suggestion } = $props()
const { id } = suggestion

Describe the proposed solution

I would love to know the reasoning behind this not being supported currently. If the issue is reactivity, I believe reactivity can be achieved using $derived or $state and $effect, like so:

const { suggestion } = $props()
const { id } = $derived(suggestion)

which is how it could work under the hood.

So, to state the obvious, my proposed solution is to...allow nested destructuring from $props().

const { suggestion: { id } } = $props()

It can be done in other parts of svelte, albeit without reactivity, and would make a few people's lives a fair bit easier.

Importance

would make my life easier

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions