Skip to content

#snippet rest args and @render spread? #11178

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

Open
brunnerh opened this issue Apr 15, 2024 · 2 comments
Open

#snippet rest args and @render spread? #11178

brunnerh opened this issue Apr 15, 2024 · 2 comments
Milestone

Comments

@brunnerh
Copy link
Member

brunnerh commented Apr 15, 2024

Describe the problem

Rest args and spreads are not supported, the latter of which has a specific error message:

cannot use spread arguments in {@render ...} tags

Is there a reason why these patterns are not supported?

{#snippet test(...items)}
	{#each items as item}
		<p>{item}</p>
	{/each}
{/snippet}

{@render test(1, 2, 3)}

REPL

<script>
	const args = [1, 2, 3];
</script>

{#snippet test(a, b, c)}
	{a} {b} {c}
{/snippet}

{@render test(...args)}

REPL

Describe the proposed solution

Support rest/spread analogous to how they work with functions.

Importance

nice to have

@Rich-Harris
Copy link
Member

Is there a reason why these patterns are not supported?

Yeah, it doesn't work

@7nik
Copy link
Contributor

7nik commented Apr 14, 2025

That PR is turning

{@render foo(...[1, 2, count])} 

into

foo(node, ...[1, 2, $.get(count)].map(val => () => val) );

so of course, it isn't reactive. But shouldn't it be

const args = $.derived(() => [1, 2, $.get(count)]);
foo(node, ...args.map((_, i, arr) => arr[i]) );

Though, it still won't work well if the array size will change.
Edit: maybe it should just error out when the array size changes (plus mentioning in the docs) because snippets do not support resting anyway.

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