Giter Club home page Giter Club logo

Comments (2)

jasonlyu123 avatar jasonlyu123 commented on August 16, 2024

I will close this in favour of #1344 and #1852. This is a limitation of the slot props inferring. Complex slot props are hard to infer. It may seem like it works where the <slot > is. However, the type inference is done at a much higher level with some hacked-generated code. A simplified example of a similar typescript code for each block is:

function hi() {
  for (const a of list) {
    const node = anotherComponent(a).slots.a;
  }
  
  return {
    slots: {
      node: anotherComponent(inferArrayItem(list)).slots.a
    }
  }
}

But with svelte:self and generic slot props, It'll need to be something like this:

function hi<T>(items: T) {
  {
    const node = hi<T>(items: T)
  }
  
  return {
    slots: {
      node: hi(items).slots.node
    }
  }
}

Even if both #1344 and #1852 are done, which is unlikely, This is still a recursive infer which Typescript will just show an error. This is where you'll need to type the slot props on your own with the $$Slot https://github.com/dummdidumm/rfcs/blob/ts-typedefs-within-svelte-components/text/ts-typing-props-slots-events.md#typing-slots or use Snippet in svelte 5.

from language-tools.

webJose avatar webJose commented on August 16, 2024

Thanks, @jasonlyu123 for the information provided. Indeed, the use of $$Slots put things in order. I am adding the finalized demo here for others to benefit. One last thing, though: Is this "Snippet" thing documented somewhere now? While I am in Svelte v4 without any intention to move yet, I would like to have a peek, in all honesty. Thanks in advance.

Finalized Workaround

This is the BaseHierarchy component:

<script lang="ts" context="module">
    export type NodeItem<T extends NodeItem<T>> = {
        id: number;
        text: string;
        nodes?: T[];
    };
</script>

<script lang="ts" generics="TItem extends NodeItem<TItem>">
    interface $$Slots {
        default: {
            node: TItem;
        };
    }
    export let nodes: TItem[];

    $: console.log("Nodes: %o", nodes);
    $: console.log("Slots: %o", $$slots);
</script>

<ul>
    {#each nodes as node (node.id)}
        <li>
            <slot {node}>
                <span>{node.text}</span>
            </slot>
            {#if (node?.nodes?.length ?? 0) > 0}
                <svelte:self nodes={node.nodes} let:node={subNode}>
                    <slot node={subNode}>
                        <span>{subNode.text}</span>
                    </slot>
                </svelte:self>
            {/if}
        </li>
    {/each}
</ul>

This is a derived component called DerivedHierarchy:

<script lang="ts" context="module">
    export interface DerNodeItem<T extends DerNodeItem<T>> extends NodeItem<T> {
        href: string;
    };
</script>

<script lang="ts" generics="TItem extends DerNodeItem<TItem>">
    import BaseHierarchy, { type NodeItem } from "./BaseHierarchy.svelte";

    interface $$Slots {
        default: {
            node: TItem
        }
    };

    export let items: TItem[];
</script>

<BaseHierarchy nodes={items} let:node>
    <slot {node}>
        <a href={node.href}>{node.text}</a>
    </slot>
</BaseHierarchy>

Now, to use either, one must type the array. I think it is because not typing would make TypeScript type the leaf nodes in the data without the nodes property, which makes it fail the required extends constraint. Example data that can be used:

    interface TestData extends NodeItem<TestData> {}
    interface UrlData extends DerNodeItem<UrlData> {
        title?: string;
    }

    const hData: TestData[] = [
        {
            id: 1,
            text: "Item 1",
            nodes: [
                {
                    id: 101,
                    text: "Item 1.1",
                },
            ],
        },
    ];

    const urlData: UrlData[] = [
        {
            id: 1,
            text: "Item 1",
            href: "/item1",
            nodes: [
                {
                    id: 101,
                    text: "Item 1.1",
                    href: "/item1_1",
                },
            ],
        },
    ];

Using urlData in, say, DerivedHierarchy would look like this:

        <DerivedHierarchy items={urlData} let:node>
            <a href={node.href} title={node.title}><strong>{node.text}</strong></a>
        </DerivedHierarchy>

This would work with full Intellisense.

from language-tools.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.