Skip to content

More useful error message when a const is used as part of a reactive assignment #2674

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

Closed
Morklympious opened this issue May 4, 2019 · 2 comments

Comments

@Morklympious
Copy link

Hello! I've run into an issue I resolved thanks to the discord, but this seems useful for anyone coming into v3 (either from v2 or another framework).

Here's a small component I was experiencing trouble with:

<script>
    export const position = "TR";

    const stances = {
        TL : "10 10",
        TR : "90 10",
        BL : "10 90",
        BR : "90 90",
    };

    $: stance = stances[position];
</script>

<style>
    .svg {
        width: 3rem;
        height: 3rem;
        padding: 1rem;
    }
</style>


<svg class="svg" viewBox="0 0 100 100">
    <g class="group" fill="gray">
        <polygon points="0 50, 50 0, 100 50, 50 100" />
        <path d="{stance}" stroke="black" stroke-width="2"/>
    </g>
</svg>

(This repros in REPL)

The error I get when I try to compile this component is ReferenceError: stance is not defined. As it turns out, the solution is changing the stances object from a const to a let. It wasn't immediately obvious to me that all components of a reactive assignment must be mutable, so I think an error that calls that out will save a lot of headaches.

Something along the lines of "A reactive assignment cannot use variables declared with const".

@Morklympious
Copy link
Author

Oops. I'm realizing now that I declared TWO const variables and that's why it was yelling. my want for a better error message still stands, but I can get away with a const map if my position is a let

@EmilTholin
Copy link
Member

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

3 participants