Unexpected Error when Destructing $store Values
See original GitHub issueDescribe 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
- Start with this REPL: link
- Download it and add TS support to the project: link
- Modify
App.svelte
by addinglang="ts"
to the opening<script>
tag. (see the above snippet) - 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.
System (please complete the following information):
- OS: Windows
- IDE: VSCode (Windows-Linux Subsystem)
- Plugin/Package:
- Svelte for VSCode
- svelte-check
- @tsconfig/svelte
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 😄
From input
svelte2tsx
generates the output:The problem is that the
$
-assignment is wrapped in a function, so the value does not exist outside of its scope.