Svelte: Undefined error when using a destructured variable

Created on 27 Sep 2019  路  3Comments  路  Source: sveltejs/svelte

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.

bug

Most helpful comment

This will be fixed by #3539.

All 3 comments

I have also come across this problem. Although not a big issue as you can simply move the export... statement up it would be good to have it work regardless.

This will be fixed by #3539.

Fixed by #3539, test added in #3722.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

robnagler picture robnagler  路  3Comments

AntoninBeaufort picture AntoninBeaufort  路  3Comments

mmjmanders picture mmjmanders  路  3Comments

noypiscripter picture noypiscripter  路  3Comments

sskyy picture sskyy  路  3Comments