Skip to content

Svelte5 5: VSCode Migrator adding $state rune to bind:this variablesΒ #13904

@KieranP

Description

@KieranP

Describe the bug

Have component that binds textarea to variable for use on afterMount

<script lang="ts">
  import { onMount } from 'svelte'

  export let autofocus = false

  let textarea: HTMLTextAreaElement

  onMount(() => {
    if (autofocus) {
      textarea.focus()
    }
  })
</script>

<textarea bind:this={textarea}></textarea>

This is being converted into this:

<script lang="ts">
  import { onMount } from 'svelte'

  interface Props {
    autofocus?: boolean;
  }

  let { autofocus = false }: Props = $props();

  let textarea: HTMLTextAreaElement = $state()

  onMount(() => {
    if (autofocus) {
      textarea.focus()
    }
  })
</script>

<textarea bind:this={textarea}></textarea>

But defining let textarea is throwing this typescript error:

Type 'HTMLTextAreaElement | undefined' is not assignable to type 'HTMLTextAreaElement'.
  Type 'undefined' is not assignable to type 'HTMLTextAreaElement'.

Manually changing let textarea: HTMLTextAreaElement = $state() to just let textarea: HTMLTextAreaElement fixes the issue.

Reproduction

See above

Logs

No response

System Info

System:
    OS: macOS 15.0.1
    CPU: (12) arm64 Apple M3 Pro
    Memory: 259.27 MB / 18.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.15.1 - ~/.asdf/installs/nodejs/20.15.1/bin/node
    Yarn: 4.3.1 - ~/.asdf/installs/nodejs/20.15.1/bin/yarn
    npm: 10.8.2 - ~/.asdf/plugins/nodejs/shims/npm
    bun: 1.1.0 - ~/.bun/bin/bun
  Browsers:
    Chrome: 130.0.6723.70
    Safari: 18.0.1
  npmPackages:
    svelte: ^5.1.0 => 5.1.0

Severity

annoyance

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions