Skip to content

Svelte 5: Variadic snippets #9672

Closed
@Rich-Harris

Description

@Rich-Harris

Describe the problem

Snippets take a single argument. This is for two reasons:

  • we initially thought it would be difficult to type variadic snippets
  • a vague notion that it might be easier to optimise snippets (e.g. hoisting them) if we know that they always have zero or one arguments

Describe the proposed solution

Hat-tip to @tcc-sejohnson for solving the typing issue:

type Snippet<Args = any, This = any> = Args extends any[] ? (this: This, ...args: Args) => unknown : (this: This, arg: Args) => unknown;

type MyCustomSnippet = Snippet<[firstName: string, lastName: string, dob: Date]>;
type MyCustomSnippetWithOneArg = Snippet<{ firstName: string, lastName: string, dob: Date }>;

I haven't thought deeply about the optimisation question. It may be a non-issue.

Alternatives considered

not doing this

Importance

nice to have

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