KEMBAR78
TS errors when destucturing in a reactive statement without declaring variables first · Issue #1938 · sveltejs/language-tools · GitHub
Skip to content

TS errors when destucturing in a reactive statement without declaring variables first #1938

@brandonmcconnell

Description

@brandonmcconnell

Describe the bug

When using Svelte Language Tools, TypeScript errors are encountered when destructuring an object in a reactive statement without explicitly declaring the variables first. This occurs even when TypeScript should be able to infer the types and assignments correctly. The expected behavior is that the destructuring should work without any TypeScript errors if the types can be inferred.

** This is a NEW bug, and this was working correctly up until recently. I am not sure of when this bug first appeared.

Reproduction

<script lang="ts">
  const person: {
    name: string;
    age: number;
  } = {
    name: "John Doe",
    age: 30,
  };

  $: ({ name, age } = person);
</script>

In this example, I don't immediately see the same issue, but I do see that the type for age is inferred as an implicit any.

To contrast, note that declaring the variables before destructuring correctly types age as number:

<script lang="ts">
  const person: {
    name: string;
    age: number;
  } = {
    name: "John Doe",
    age: 30,
  };

  let name: string;
  let age: number;

  $: ({ name, age } = person);
</script>

Expected behaviour

The types should be properly inferred when creating variables via a reactive statement without needing to declare them beforehand.

System Info

  • OS: macOS Ventura v13.1 (22C65)
  • IDE: VSCode v1.76.1 (Universal)

Which package is the issue about?

Svelte for VS Code extension

Additional Information, eg. Screenshots

Screen recording:

Screen.Recording.2023-03-14.at.3.41.10.PM.mov

It's also noteworthy that if I first initialize the variables this way, I can then destructure them the same way I did before and not get the same errors:

Screenshot 2023-03-14 at 3 44 39 PM

Metadata

Metadata

Assignees

No one assigned

    Labels

    FixedFixed in master branch. Pending production release.bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions