You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I need to pass snippets that take arguments across component boundaries, and for that, the Snippet type provided by svelte currently is not enough, there needs to be a utility function.
Currently I have this function that works, but Ideally something like this should be exported by svelte:
// Extract all parameters of a SnippettypeSnippetParams<T>=TextendsSnippet<infer P> ? P : never;// Helper type to create tuple of functions returning each type from a tupletypeTupleToFunctionTuple<Textendsany[]>={[KinkeyofT]: ()=>T[K];};// Helper type to make it single function or tuple of functions based on lengthtypeParamFnsType<Textendsany[]>=Textends[any]
? ()=>T[0]// Single parameter case
: TupleToFunctionTuple<T>;// Multiple parameters case/** * Converts a multi-parameter snippet into a single parameter snippet by providing fixed values for additional parameters * * @example * * // Original snippet with multiple parameters * {#snippet MainSnip(param1: string, param2: string)} * <div>{param1} {param2}</div> * {/snippet} * * // Convert to single param snippet with fixed second param * const SnippetWithParam = bindSnippetArgs(MainSnip, () => "Bruzz") * // or with multiple fixed params * const SnippetWithParam2 = bindSnippetArgs(MainSnip, [() => "Bruzz", () => "Hello"]) * * // Can now use with just one parameter * banner({title: "Hello"}, SnippetWithParam) * banner({title: "Hello"}, SnippetWithParam2) * * @param snippet - The original multi-parameter snippet to convert * @param paramFns - Function or array of functions that return fixed values for additional parameters * @returns A snippet that can be called with a `@render` tag */exportfunctionbindSnippetArgs<TextendsSnippet<any[]>>(snippet: T,paramFns: ParamFnsType<SnippetParams<T>>){constfnsArray=Array.isArray(paramFns) ? paramFns : [paramFns];// @ts-expect-error Snippet typing is cookedreturn((anchor: any)=>snippet(anchor, ...fnsArray))asSnippet;}
Importance
would make my life easier
The text was updated successfully, but these errors were encountered:
Describe the problem
I need to pass snippets that take arguments across component boundaries, and for that, the
Snippet
type provided by svelte currently is not enough, there needs to be a utility function.Describe the proposed solution
REPL Showcase
Currently I have this function that works, but Ideally something like this should be exported by svelte:
Importance
would make my life easier
The text was updated successfully, but these errors were encountered: