ComponentInstance type doesn't include exposed methods of generic components
Vue version
3.4.8
Link to minimal reproduction
Steps to reproduce
- In the file
App.vue
, notice how TypeScript complains that the functiongetItemAtIndex
(which is exposed by the generic componentOrderedList.vue
) doesn't exist in the component instance, which is typed using theComponentInstance
type added in Vue 3.4.
What is expected?
ComponentInstance
type should include exposed component methods in type.
What is actually happening?
ComponentInstance
type doesn't include exposed component methods in type when working with generic components, so TypeScript complains that there is an error when trying to access them.
System Info
No response
Any additional comments?
I'm aware that there is a type ComponentExposed
in the package vue-component-type-helpers
that allows typing generic component instances, but I think ComponenteInstance
should also consider this use case when working with generics so that there is no need to rely on external packages.
I've been doing some more digging and the issue seems to be caused by the fact that typeof GenericComponent<TypeParameter>
(in the case of the provided example, this would be typeof OrderedList<ListItem>
) matches the following type condition used when defining ComponentInstance
: T extends FunctionalComponent<infer Props, infer Emits>
, which doesn't seem intended.
The issue seems to be caused by Vue/language-tools
and ComponentInstance
from vue
.
After https://github.com/vuejs/core/pull/9556 lands into 3.5 I'll work on tweaking language tools to support the new helpers added. Currently the PR by itself won't be closing this issue.
I also encountered this problem, to temporarily remove error, I use
ref<Parameters<NonNullable<Parameters<typeof ScrollList>['2']>>['0'] | null>(null);
replace
ref<InstanceType<typeof ScrollList> | null>(null);
Although this looks a bit strange, but it can give me correct tip when I use generic
in script.