question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unexpected Error when Destructing $store Values

See original GitHub issue

Describe the bug The statement $: ({ value } = $store); is valid syntax when value is a property of $store and $store is a Svelte store. It behaves like normal destructuring and is reactive to store updates. This produces an error when lang="ts".

# App.svelte
------------
<script lang="ts">
  import data from "./store";
  $: ({ count } = $data);
</script>
jd:/svelte-playground $ yarn svelte-check                                                                                                                                                                                                                      [0:30]
yarn run v1.22.4
warning package.json: No license field
$ /home/jd/Projects/svelte-playground/node_modules/.bin/svelte-check

Loading svelte-check in workspace: /home/jd/Projects/svelte-playground
Getting Svelte diagnostics...
====================================

/home/jd/Projects/svelte-playground/src/App.svelte:3:9
Error: No value exists in scope for the shorthand property 'count'. Either declare one or provide an initializer. (ts)
;
  $: ({ count } = $data

====================================
svelte-check found 1 error and 0 warnings
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

To Reproduce

  1. Start with this REPL: link
  2. Download it and add TS support to the project: link
  3. Modify App.svelte by adding lang="ts" to the opening <script> tag. (see the above snippet)
  4. run svelte-check

Expected behavior This is valid Svelte/JS syntax and his code runs fine. It should not produce typechecker errors.

Screenshots If applicable, add screenshots to help explain your problem. image

System (please complete the following information):

  • OS: Windows
  • IDE: VSCode (Windows-Linux Subsystem)
  • Plugin/Package:

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
dummdidummcommented, Aug 11, 2020

The problem is the ts AST analysis. It only does the let x = ... transformation if it’s a binary expression, but in this case it’s a binary expression wrapped inside a parenthized expression.

Edit: I’m in the middle of fixing this - all these if-then-also-think-of-this-corner-case-checks make my mind blurry 😄

1reaction
dummdidummcommented, Aug 11, 2020

From input

<script lang="ts">
  import data from "./store";
  $: ({ count } = $data);
</script>

<main>
  <h1>Count (inline): {$data.count}</h1>
  <h1>Count (reactive destructure): {count}</h1>
	<button on:click={() => data.update(c => ({count: c.count + 1}))}>
    Increment
  </button>
</main>

svelte2tsx generates the output:

<></>;
import data from "./store";
function render() {

  
  ;() => {$: ({ count } = __sveltets_store_get(data));}
;
() => (<>

<main>
  <h1>Count (inline): {__sveltets_store_get(data).count}</h1>
  <h1>Count (reactive destructure): {count}</h1>
	<button onclick={() => data.update(c => ({count: c.count + 1}))}>
    Increment
  </button>
</main> </>);
return { props: {}, slots: {}, getters: {}, events: {} }}

export default class extends createSvelte2TsxComponent(__sveltets_partial(render)) {
}

The problem is that the $-assignment is wrapped in a function, so the value does not exist outside of its scope.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Destructuring assignment cause error: "unexpected token ="
around the assignment statement is required syntax when using object literal destructuring assignment without a declaration.
Read more >
Prevent Error with Default {} when Destructuring
When you use destructuring, make sure to set a default value of empty {} to prevent it from throwing an error! function hi(person)...
Read more >
Destructuring assignment - JavaScript - MDN Web Docs
The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from ...
Read more >
Safer Code with ES6 Destructuring — Explained in One ...
Uncaught TypeError: Cannot read property 'x' of undefined. Recognize this? Seen this before? It is perhaps the most common error in Javascript.
Read more >
JavaScript object destructuring usages you must know
Introduction We use JavaScript objects to store data and retrieve it later. We store... Tagged with javascript, webdev, beginners, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found