svelte icon indicating copy to clipboard operation
svelte copied to clipboard

isSnippet() function

Open webJose opened this issue 1 year ago • 4 comments

Describe the problem

We use a class that accepts (or tries to, at least) 3 types of content in its content property:

  • A component
  • A snippet
  • A single-spa parcel

The class used to work fine with custom isSnippet and isParcel functions. The former one was working by checking the content was an object that had a render function. This seems to have changed now. It seems that snippets are now (also?) functions.

Describe the proposed solution

Instead of us, mere mortals, having to guess what constitutes a snippet, it would be nice if Svelte provided us with an isSnippet() function that would always be accurate. Taking out the guesswork is the main concern here.

Importance

would make my life easier

webJose avatar Oct 29 '24 14:10 webJose

Related:

  • #9774

brunnerh avatar Oct 29 '24 14:10 brunnerh

That would be an interesting approach, but unsure how it would play in the TypeScript world. The idea of this function is probably simpler to achieve. I just need the following from Svelte:

export function isSnippet(obj: unknown): obj is Snippet { ... }

A simple Boolean function that narrows types in TS. The idea of a single syntax for components and snippets is cool, though. I suppose it is also harder to achieve, TS-wise.

webJose avatar Oct 29 '24 18:10 webJose

If you guys go for this (which I would love to see), may I also ask for an isComponent() function? Just to complete the set. 😄

webJose avatar Nov 28 '24 05:11 webJose

Looking forward to this!

PuruVJ avatar Nov 11 '25 08:11 PuruVJ

I also support this. Sometimes, I would like to allow a prop that can be either: a function that returns a string or a snippet:

    interface Props {
        render: ((item: T) => string) | Snippet
    }

The use case: we have quite a lot of simple formatting functions like Intl.DateTimeFormat or the like and

<Comp render={Intl.DateTimeFormat} />

is much smoother than

<Comp>
  {#snippet render(x)}
    {Intl.DateTimeFormat(x)}
  {/snippet}
</Comp>

But this lags the power of snippets -- for that reason, I'd like to allow both and using the same property name makes the API simpler.

However to render this prop, I would need to be able to distinguish between the two variations -- and that makes isSnippet a requirement.

ptrxyz avatar Jan 09 '26 13:01 ptrxyz