-
Notifications
You must be signed in to change notification settings - Fork 290
Fix generic SFC findComponent types #2934
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
Merged
cexbrayat
merged 3 commits into
vuejs:main
from
simonyang08:codex/fix-generic-sfc-find-component
Sep 4, 2026
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| import { expectType } from './index' | ||
| import type { FunctionalComponent, VNode } from 'vue' | ||
| import { defineComponent, h } from 'vue' | ||
| import { mount } from '../src' | ||
| import type { DOMWrapper, VueWrapper } from '../src' | ||
| import type WrapperLike from '../src/interfaces/wrapperLike' | ||
|
|
||
| // `vue-tsc` emits a generic SFC as a function with a generic call signature whose | ||
| // return type is `VNode & { __ctx?: ... }`. The `__ctx` property is the | ||
| // discriminator between vue-tsc generic SFCs and ordinary generic functional | ||
| // components (which return bare `VNode`). | ||
| declare const VueTscGenericSfc: <T extends string | number>( | ||
| __VLS_props: any, | ||
| __VLS_ctx?: any, | ||
| __VLS_expose?: any, | ||
| __VLS_setup?: Promise<any> | ||
| ) => VNode & { __ctx?: any } | ||
|
|
||
| // A plain generic functional component returns bare `VNode` (no `__ctx`). | ||
| declare const GenericFunctional: <T>(props: { items: T[] }) => VNode | ||
|
|
||
| const wrapper = mount(defineComponent({ template: '' })) | ||
|
|
||
| // ---- findComponent ---- | ||
| // Vue-tsc generic SFC should resolve to VueWrapper, not DOMWrapper. | ||
| const sfcFound = wrapper.findComponent(VueTscGenericSfc) | ||
| expectType<VueWrapper>(sfcFound) | ||
| // `.vm` is a VueWrapper-only API; it must type-check on the vue-tsc SFC result. | ||
| expectType<unknown>(sfcFound.vm) | ||
|
|
||
| // Plain generic functional component must still resolve to DOMWrapper; `.vm` | ||
| // should NOT type-check (use @ts-expect-error to assert this). | ||
| const fnFound = wrapper.findComponent(GenericFunctional) | ||
| expectType<DOMWrapper<Node>>(fnFound) | ||
| void ( | ||
| // @ts-expect-error -- DOMWrapper has no `vm` property. | ||
| fnFound.vm | ||
| ) | ||
|
|
||
| // ---- getComponent ---- | ||
| const sfcGot = wrapper.getComponent(VueTscGenericSfc) | ||
| expectType<Omit<VueWrapper, 'exists'>>(sfcGot) | ||
| void ( | ||
| // @ts-expect-error -- `exists` is stripped on getComponent. | ||
| sfcGot.exists | ||
| ) | ||
| expectType<unknown>(sfcGot.vm) | ||
|
|
||
| const fnGot = wrapper.getComponent(GenericFunctional) | ||
| expectType<Omit<DOMWrapper<Element>, 'exists'>>(fnGot) | ||
| void ( | ||
| // @ts-expect-error -- DOMWrapper has no `vm` property. | ||
| fnGot.vm | ||
| ) | ||
|
|
||
| // ---- findAllComponents ---- | ||
| const sfcAll = wrapper.findAllComponents(VueTscGenericSfc) | ||
| expectType<VueWrapper[]>(sfcAll) | ||
| expectType<unknown | undefined>(sfcAll[0]?.vm) | ||
|
|
||
| const fnAll = wrapper.findAllComponents(GenericFunctional) | ||
| expectType<DOMWrapper<Node>[]>(fnAll) | ||
| void ( | ||
| // @ts-expect-error -- DOMWrapper<Node>[] elements have no `vm` property. | ||
| fnAll[0]?.vm | ||
| ) | ||
|
|
||
| // A `FunctionalComponent`-typed component has a call signature returning `any` | ||
| // (see Vue's FunctionalComponent type). The `any` return must not satisfy the | ||
| // `__ctx` discriminator, so all three APIs keep resolving to DOMWrapper and | ||
| // `.vm` must not type-check. | ||
| const PlainFunctional: FunctionalComponent<{ a: string }> = props => | ||
| h('div', props.a) | ||
|
|
||
| // ---- findComponent (FunctionalComponent shape) ---- | ||
| const fcFound = wrapper.findComponent(PlainFunctional) | ||
| expectType<DOMWrapper<Node>>(fcFound) | ||
| void ( | ||
| // @ts-expect-error -- DOMWrapper has no `vm` property. | ||
| fcFound.vm | ||
| ) | ||
|
|
||
| // ---- getComponent (FunctionalComponent shape) ---- | ||
| const fcGot = wrapper.getComponent(PlainFunctional) | ||
| expectType<Omit<DOMWrapper<Element>, 'exists'>>(fcGot) | ||
| void ( | ||
| // @ts-expect-error -- DOMWrapper has no `vm` property. | ||
| fcGot.vm | ||
| ) | ||
|
|
||
| // ---- findAllComponents (FunctionalComponent shape) ---- | ||
| const fcAll = wrapper.findAllComponents(PlainFunctional) | ||
| expectType<DOMWrapper<Node>[]>(fcAll) | ||
| void ( | ||
| // @ts-expect-error -- DOMWrapper<Node>[] elements have no `vm` property. | ||
| fcAll[0]?.vm | ||
| ) | ||
|
|
||
| // The WrapperLike interface mirrors the BaseWrapper overloads, including the | ||
| // `any`-return guard. | ||
| const wrapperLike: WrapperLike = wrapper | ||
| expectType<DOMWrapper<Element>>(wrapperLike.findComponent(PlainFunctional)) | ||
| expectType<DOMWrapper<Node>[]>(wrapperLike.findAllComponents(PlainFunctional)) | ||
| expectType<Omit<DOMWrapper<Element>, 'exists'>>( | ||
| wrapperLike.getComponent(PlainFunctional) | ||
| ) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be better to use a real
<script setup generic="Item">component in a .vue file imported by the test, instead of relying on internals of vue-tsc