Skip to content

Expose types of locally registered components #3367

Closed
@gertqin

Description

@gertqin

What problem does this feature solve?

Currently IDE extensions cannot extract the types of locally registered components, which makes it difficult to support strong typings of components inside SFC templates, see vuejs/language-tools#80.

What does the proposed API look like?

Expose the component types as generics (if possible):

export declare interface ComponentOptionsBase<LocalComponents extends Record<string, Component>, Props, ...> {
    components?: LocalComponents;
    ...
}

Activity

HcySunYang

HcySunYang commented on Mar 9, 2021

@HcySunYang
Member

Hi @johnsoncodehk , @gertqin . Can you two share more context or information? Let me know what we should do. Thanks~

gertqin

gertqin commented on Mar 9, 2021

@gertqin
Author

I think what is needed is something like this

import OtherComponent from "./OtherComponent.vue";

const myComponent = defineComponent({
  components: {
    OtherComponent
  },
  setup() {
  // ...
  }
})

type ExtractLocalComponents<T> = T extends DefineComponent<
  any,
  any,
  ...
  infer LC
>
  ? LC
  : never;

type LocalComponents = ExtractLocalComponents<typeof myComponent>;
// LocalComponents = { "OtherComponent": typeof OtherComponent }

That is, be able to extract the types of the locally registered components from the exported component similarly to props and emits (#2094). @johnsoncodehk is this correct?

johnsoncodehk

johnsoncodehk commented on Mar 9, 2021

@johnsoncodehk
Member

@gertqin I need to avoid use vue 3 helper types to support others like Vue.extends / @vue/composition-api / vue-class-component etc, so I would like to do this:

type LocalComponents = typeof myComponent extends { components?: infer C } ? C : unknown;
// LocalComponents = { "OtherComponent": typeof OtherComponent }
added
has PRA pull request has already been submitted to solve the issue
on Oct 21, 2023
added a commit that references this issue on Apr 25, 2024
4cc9ca8
locked and limited conversation to collaborators on Aug 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

has PRA pull request has already been submitted to solve the issuescope: types

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Participants

    @pikax@HcySunYang@johnsoncodehk@gertqin

    Issue actions

      Expose types of locally registered components · Issue #3367 · vuejs/core