Skip to content

Infer type of slots from $slots #539

@yusufkandemir

Description

@yusufkandemir

The current system analyzes the available slots in different ways and does a good job at that. However, this is not possible for external components(e.g. UI component libraries).

From vuejs/rfcs#192 (comment):

johnsoncodehk (Johnson Chu) on 8 Oct 2020:
But for a third party library like vue-router, I can't read the typescript source code to calculate v-slot types, for this case I need to define additional options for my tool like this: johnsoncodehk/volar#v-slot-type-checking

vuejs/core#4465 allows typing the slots, along with lots of other changes. So, after that gets merged, $slots will basically contain the typed slots.
https://github.com/pikax/vue-next/blob/e201d2f046c46dc1fa26551ce28ac1a322ed1ad9/packages/runtime-core/src/componentPublicInstance.ts#L422

Volar is already inferring the props types from $props:
https://github.com/johnsoncodehk/volar/blob/aa66f9fdc0b1752e58c56d680c942b9d2a3787ec/packages/vscode-vue-languageservice/src/utils/globalDoc.ts#L83-L90

So, in a similar way, $slots can be inferred too, then the current slot discovering system can do its job to enrich the types.

Currently, the UI library/framework authors can ship the constructor types themselves, without depending on current Vue types. An example:
https://github.com/quasarframework/quasar/blob/76c0c841554751a5be743b0302523b63f956ba25/ui/types/ts-helpers.d.ts#L33-L37
(thanks for some imagination from #418 (comment) btw)

So, since this feature is not blocked by vuejs/core#4465 because there are currently ways to provide that type, I think this can be implemented right away! 🚀

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions