KEMBAR78
TypeScript an't infer type of store contents · Issue #1950 · sveltejs/language-tools · GitHub
Skip to content

TypeScript an't infer type of store contents #1950

@lucassilvas1

Description

@lucassilvas1

Describe the bug

In the snippet below, I can't use $ to access the contents of store. I'm not sure if this is a Svelte or a TypeScript problem, I suspect Svelte:

<script>
import type { Writable } from "svelte/store";

export let store: Writable<string[]> | null = null;

  if (store) {
    $store.length
  }
</script>

TypeScript tells me that "Property 'length' does not exist on type 'never'.", even though the type of $store should be string[] inside the if block.

Expected Behavior
TypeScript infers the type of $store as string[] inside the if block.

Actual Behavior
TypeScript infers the type of $store as null inside the if block.

Reproduction

Copying the script above should be enough

Logs

No response

System Info

System:
    OS: Windows 10 10.0.19045
    CPU: (12) x64 AMD Ryzen 5 5600X 6-Core Processor
    Memory: 16.32 GB / 31.91 GB
  Binaries:
    Node: 16.13.0 - C:\Program Files\nodejs\node.EXE
    npm: 8.4.1 - ~\node_modules\.bin\npm.CMD
  Browsers:
    Edge: Spartan (44.19041.1266.0), Chromium (111.0.1661.44)    
  npmPackages:
    rollup: ^2.3.4 => 2.79.1
    svelte: ^3.0.0 => 3.55.1

Severity

annoyance

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