sveltejs / language-tools

The Svelte Language Server, and official extensions which use it
MIT License
1.25k stars 201 forks source link

Svelte 5: Binding on Generic Props shows error #2494

Closed nadanke closed 1 month ago

nadanke commented 2 months ago

Describe the bug

I have a component that uses generics and takes a bindable prop that looks like this:

{
    current: T;
    default: T;
    error: string | null;
}

This worked fine until I upgraded, now it shows errors in both Neovim and VSCode and also in svelte-check. It still works the same but every time the component is used it shows an error: Type '{ current: unknown; default: unknown; error: string | null; }' is not assignable to type '{ current: number; default: number; error: null; }'. Types of property 'current' are incompatible. Type 'unknown' is not assignable to type 'number'.

svelte-check didn't show the error until i ran this upgrade:

devDependencies:
- svelte-check 3.8.6
+ svelte-check 4.0.1
- typescript-eslint 8.3.0
+ typescript-eslint 8.5.0

but the error still shows up in Neovim/Code even before the upgrade, so I don't think the problem is in svelte-check itself, which is why I'm creating the issue here.

Reproduction

I have not been able to reproduce this in the REPL but it's not a lot of code. The component:

<script lang="ts" generics="T">
    let {
        input = $bindable()
    }: {
        input: {
            current: T;
            default: T;
            error: string | null;
        };
    } = $props();
</script>

then use normally

<script lang="ts">
    import MyComp from '$lib/MyComp.svelte';

    let x = $state({ current: 1, default: 2, error: null });
</script>

<MyComp bind:input={x} />

It works if you don't bind it, or if you pass in {x as typeof x}, which is weird

Logs

No response

System Info

System:
    OS: Linux 6.10 Arch Linux
    CPU: (16) x64 12th Gen Intel(R) Core(TM) i5-1240P
    Memory: 22.74 GB / 31.06 GB
    Container: Yes
    Shell: 5.2.32 - /bin/bash
  Binaries:
    Node: 22.8.0 - /usr/bin/node
    Yarn: 1.22.22 - /usr/bin/yarn
    npm: 10.2.4 - ~/.local/share/pnpm/npm
    pnpm: 9.1.1 - ~/.local/share/pnpm/pnpm
    bun: 1.1.4 - ~/.bun/bin/bun
  Browsers:
    Brave Browser: 128.1.69.162
  npmPackages:
    svelte: 5.0.0-next.244 => 5.0.0-next.244

Severity

annoyance

dummdidumm commented 2 months ago

Result of #2477 - we silence type errors due to the whole type being unknown, but not if one of its members is unknown.