Improving Type Safety in defineSlots
#12478
chadwickellis
started this conversation in
General Discussions
Replies: 1 comment
-
Maybe slots could be defined in a simpler way, ex. const slots = defineSlots<{
default: { msg: string }
}>() The generic type doesn't match the type of the const emit = defineEmits<{
change: [id: number]
update: [value: string]
}>() |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
The current documentation for the
defineSlots
macro offers the following example:This example works well for defining slot props, but it raises questions about the return type of the slot function, which is currently typed as
any
. While this is sufficient for many use cases, could it be improved for scenarios where developers interact with slots programmatically in a component’s script?For instance, consider the alternative of leveraging the
Slot
type from the@vue/runtime-core package
. This type explicitly represents a slot function and can be used as follows:Questions for the Community
Should the Return Type Be More Explicit?
Future Considerations
Documentation Enhancements
Slot
be added to the documentation to cater to developers seeking higher type safety in specific use cases?Example Use Cases
Here’s a comparison of how the current approach and the
Slot
-based approach handle programmatic slot interactions:Using
any
(Current Documentation)Using
Slot
Inviting Feedback
I’d love to hear thoughts from the community:
Beta Was this translation helpful? Give feedback.
All reactions