Skip to content

Discussion: Allow <svelte:self> as slot without if/each block? #2798

@trbrc

Description

@trbrc

The API docs say the following about <svelte:self>:

It cannot appear at the top level of your markup; it must be inside an if or each block to prevent an infinite loop.
https://svelte.dev/docs#svelte_self

However, there is at least one case where unbounded recursion can be prevented, outside of #if/#each, which is when <svelte:self> is used as a slot in another component. That component may expose the slot conditionally in a way that restricts recursion depth.

See REPL for a silly example that would run if the compiler allowed it: https://svelte.dev/repl/6df2eaa40872455d9ab9487351687281?version=3.4.1

Here I've wrapped it in a pointless if just to appease the compiler:
https://svelte.dev/repl/bca90979b8e444cdb05b13969cb6e601?version=3.4.1

If the purpose is to prevent guaranteed infinite loops, rather than risk of infinite loops, the compiler should arguably allow a pattern like this too.

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