Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Slots generic parameter to DefineComponent #5961

Closed
DrJume opened this issue May 19, 2022 · 2 comments
Closed

Add Slots generic parameter to DefineComponent #5961

DrJume opened this issue May 19, 2022 · 2 comments
Labels

Comments

@DrJume
Copy link
Contributor

DrJume commented May 19, 2022

What problem does this feature solve?

Currently, to define a Vue component with typed slots, you can't use DefineComponent as the slots are manually set to Readonly<InternalSlots>.

A hacky workaround is to use a generator function that returns defineComponent(...).
The return type of this generator function is a constructor type which tries to emulate DefineComponent:

<script lang="tsx">
export createGenericListComponent<T extends { id: string }>(): {
  new (): {
    $props: {
      items: T[]
    }
    $slots: {
      default: ({ item }: { item: T }) => VNode[]
    }
  }
} {
  return defineComponent({
    props: {
      items: Array as PropType<T[]>
    },
    setup(props, {slots}) {
      return () => (
        <ul>
          {props.items.map((item) => (
            <li key={item.id}>
                {slots.default && slots.default({ item })}
            </li>
          ))}
        </ul>
      )
    }
  })
}
</script>

What does the proposed API look like?

For manually setting the type:

export createGenericListComponent<T extends { id: string }>(): DefineComponent<
  { items: T[] }, // Props
  { default: ({ item }: { item: T }) => VNode[] } // Slots
>

In the future it would be awesome, if Slots would be infered out of the template / render function.

Or a defineSlots<...>() macro could be provided instead, like with defineProps<...>()?

@DrJume DrJume added the ✨ feature request New feature or request label May 19, 2022
@phy-lei
Copy link

phy-lei commented Jan 4, 2023

I also expect this feature to be implemented in the feature, especially slot can be infered out

@sxzz
Copy link
Member

sxzz commented Jan 4, 2023

FYI Vue Macros implemented this feature.

https://vue-macros.sxzz.moe/macros/define-slots.html

relate RFC #2693

@DrJume DrJume closed this as completed Sep 3, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Sep 19, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants