Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

invalid-binding error in odd places #8107

Open
brunnerh opened this issue Dec 12, 2022 · 0 comments
Open

invalid-binding error in odd places #8107

brunnerh opened this issue Dec 12, 2022 · 0 comments

Comments

@brunnerh
Copy link
Member

Describe the bug

I tried to fix a typing issue here by extracting the index access to a @const but this raised an invalid-binding in a place where I would not expect it and also did not raise it where I would have expected it.

Reproduction

<script lang="ts">
    import Attribute from './attribute.svelte';

    type Field = string | number | boolean;
    type FieldArray = Array<Field>;

    function isFieldArray(value: Field | FieldArray): value is FieldArray {
        return Array.isArray(value);
    }

    type ExampleObject = {
        document: Record<string, Field | FieldArray>;
    };

    const obj: ExampleObject = {
        document: {
            name: 'John',
            age: 30,
            isMarried: true,
            hobbies: ['coding', 'reading', 'gaming']
        }
    };
</script>

{#each Object.keys(obj.document) as k}
    {@const value = obj.document[k]}
    {#if isFieldArray(value)}
        {#each value as _, index}
            <!-- Cannot bind to a variable declared with {@const ...} -->
            <input bind:value={value[index]} />
        {/each}
    {:else}
        <!-- No error here -->
        <Attribute bind:value={value} />
    {/if}
{/each}
<!-- attribute.svelte -->
<script lang="ts">
	export let value: string | number | boolean;
</script>
{value}

The first binding accesses value[index] and thus does not try to re-assign value itself, yet there is an error.
The second binding should try to re-assign value, yet there is no error.

Logs

No response

System Info

System:
    OS: Windows 10 10.0.19045
    CPU: (12) x64 Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
    Memory: 18.14 GB / 31.92 GB
  Binaries:
    Node: 16.16.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.17 - ~\AppData\Roaming\npm\yarn.CMD
    npm: 6.14.13 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Chrome: 108.0.5359.99
    Edge: Spartan (44.19041.1266.0), Chromium (108.0.1462.46)
    Internet Explorer: 11.0.19041.1566
  npmPackages:
    svelte: ^3.54.0 => 3.54.

Severity

annoyance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant