Skip to content

Undefined error when using a destructured variable #3628

Closed
@ghost

Description

Describe the bug
When trying to render a child component Svelte will think that a destructured variable is undefined when another export ... statement comes after the destructuring.

Logs
In the example code given below you should see the error:

bar is not defined

when the baz export is at the bottom of the <script> and you should see it fixed when the baz export is moved to the top.

To Reproduce
Create a SubCom.svelte and add this code:

<p>{bar}</p>

<script>
export let foo
const { bar } = foo

export let baz
</script>

Now in App.svelte add this code:

<h1>Syntax related error</h1>

<SubCom {foo} {baz} />

<script>
import SubCom from './SubCom.svelte'
	
const foo = { bar: 1 }
const baz = {}
</script>

You should see the error.

To fix the error just change SubCom.svelte to:

<p>{bar}</p>

<script>
export let baz
export let foo
const { bar } = foo
</script>

Expected behavior
I expect both versions to work.

Severity
Not high, since it's easy to work around.

You could also fix by removing the destructuring syntax.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions