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

Expose types of locally registered components #3367

Open
gertqin opened this issue Mar 5, 2021 · 3 comments · Fixed by #3399
Open

Expose types of locally registered components #3367

gertqin opened this issue Mar 5, 2021 · 3 comments · Fixed by #3399
Assignees
Labels
has PR A pull request has already been submitted to solve the issue scope: types

Comments

@gertqin
Copy link

gertqin commented Mar 5, 2021

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;
    ...
}
@HcySunYang
Copy link
Member

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

@gertqin
Copy link
Author

gertqin commented Mar 9, 2021

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
Copy link
Member

johnsoncodehk commented Mar 9, 2021

@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 }

@pikax pikax added the has PR A pull request has already been submitted to solve the issue label Oct 21, 2023
yyx990803 pushed a commit that referenced this issue Apr 25, 2024
…s and respect `expose` on defineComponent (#3399)

close #3367
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
has PR A pull request has already been submitted to solve the issue scope: types
Projects
None yet
4 participants