Skip to content

Support top-level {@const …} #16355

@regexident

Description

@regexident

Describe the problem

Currently the only way to have a top-level {@const …} to avoid redundant computation …

<script lang="ts">
    // ...
</script>

<ul>
    <li>{expensiveComputation().foo}</li>
    <li>{expensiveComputation().bar}</li>
    <li>{expensiveComputation().baz}</li>
</ul>

… is to wrap everything in a silly {#if true} … {/if} block:

<script lang="ts">
    // ...
</script>

{#if true}
    {@const result = expensiveComputation()}
    <ul>
        <li>{result.foo}</li>
        <li>{result.bar}</li>
        <li>{result.baz}</li>
    </ul>
{/if}

Describe the proposed solution

Given that the above silly hack works I see no reason why there shouldn't be first-class support for it:

<script lang="ts">
    // ...
</script>

{@const result = expensiveComputation()}
<ul>
    <li>{result.foo}</li>
    <li>{result.bar}</li>
    <li>{result.baz}</li>
</ul>

Importance

would make my life easier

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