Skip to content

Reactive, destructured declarations aren't recognised by TypeScript in certain combinations #1604

@sxxov

Description

@sxxov

Describe the bug

When writing reactive, destructured declarations (using the $: directive), with certain combinations of renames & default initialisers, TypeScript would be confused into thinking it's a variable assignment expression instead of a declaration statement.

Reproduction

<script lang="ts">
	// 1. OK  → $: ({ a = 1, c: d = 2 } = { a: 0, c: 0 });
	// 2. OK  → $: ({ a: b = 1, c: d } = { a: 0, c: 0 });
	// 3. OK  → $: ({ a: b = 1, c = 2 } = { a: 0, c: 0 });
	// 4. OK  → $: ({ a: b = 1, c: d, e: f = 2 } = { a: 0, c: 0, e: 0 });
	// 5. NOK → $: ({ a: b = 1 } = { a: 0 });
	// 6. NOK → $: ({ a: b = 1, c: d = 2 } = { a: 0, c: 0 });

	// Using no. 5 as a runnable example that fails:
	$: ({ a: b = 1 } = { a: 0 });
	//       ^ Cannot find name 'b'. ts(2304)
</script>

Expected behaviour

<script lang="ts">
	// 1. OK  → $: ({ a = 1, c: d = 2 } = { a: 0, c: 0 });
	// 2. OK  → $: ({ a: b = 1, c: d } = { a: 0, c: 0 });
	// 3. OK  → $: ({ a: b = 1, c = 2 } = { a: 0, c: 0 });
	// 4. OK  → $: ({ a: b = 1, c: d, e: f = 2 } = { a: 0, c: 0, e: 0 });
	// 5. OK  → $: ({ a: b = 1 } = { a: 0 });
	// 6. OK  → $: ({ a: b = 1, c: d = 2 } = { a: 0, c: 0 });

	// Using no. 5 as a runnable example that shouldn't fail:
	$: ({ a: b = 1 } = { a: 0 });
	//       ^ let b: number
</script>

System Info

  • OS: Windows 11 21H2
  • IDE: VSCode 1.70.1

Which package is the issue about?

svelte-language-server

Additional Information, eg. Screenshots

image

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