-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
fix(types): fix generic inference in defineComponent #13770
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
Open
OlTrenin
wants to merge
2
commits into
vuejs:main
Choose a base branch
from
OlTrenin:OlTrenin/fix/13763/generic-props-inference
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+42
−1
Open
Changes from 1 commit
Commits
Show all changes
2 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
151 changes: 151 additions & 0 deletions
151
packages/runtime-core/__tests__/defineComponentGeneric.spec.ts
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,151 @@ | ||
/** | ||
* @vitest-environment jsdom | ||
*/ | ||
import { | ||
defineComponent, | ||
h, | ||
nodeOps, | ||
ref, | ||
render, | ||
serializeInner, | ||
} from '@vue/runtime-test' | ||
import { describe, expect, test } from 'vitest' | ||
|
||
describe('defineComponent with generic functions', () => { | ||
test('should preserve type inference for generic functions with props option', () => { | ||
const GenericComp = defineComponent( | ||
<T extends string | number>(props: { value: T; items: T[] }) => { | ||
const count = ref(0) | ||
return () => | ||
h('div', `${props.value}-${props.items.length}-${count.value}`) | ||
}, | ||
{ props: ['value', 'items'] }, | ||
) | ||
|
||
expect(typeof GenericComp).toBe('object') | ||
expect(GenericComp).toBeDefined() | ||
|
||
const root1 = nodeOps.createElement('div') | ||
render(h(GenericComp, { value: 'hello', items: ['world'] }), root1) | ||
expect(serializeInner(root1)).toBe(`<div>hello-1-0</div>`) | ||
|
||
const root2 = nodeOps.createElement('div') | ||
render(h(GenericComp, { value: 42, items: [1, 2, 3] }), root2) | ||
expect(serializeInner(root2)).toBe(`<div>42-3-0</div>`) | ||
}) | ||
|
||
test('should work with complex generic constraints', () => { | ||
interface BaseType { | ||
id: string | ||
name?: string | ||
} | ||
|
||
const ComplexGenericComp = defineComponent( | ||
<T extends BaseType>(props: { item: T; list: T[] }) => { | ||
return () => h('div', `${props.item.id}-${props.list.length}`) | ||
}, | ||
{ props: ['item', 'list'] }, | ||
) | ||
|
||
expect(typeof ComplexGenericComp).toBe('object') | ||
|
||
const root = nodeOps.createElement('div') | ||
render( | ||
h(ComplexGenericComp, { | ||
item: { id: '1', name: 'test' }, | ||
list: [ | ||
{ id: '1', name: 'test' }, | ||
{ id: '2', name: 'test2' }, | ||
], | ||
}), | ||
root, | ||
) | ||
expect(serializeInner(root)).toBe(`<div>1-2</div>`) | ||
}) | ||
|
||
test('should work with emits option', () => { | ||
const GenericCompWithEmits = defineComponent( | ||
<T extends string | number>(props: { value: T }, { emit }: any) => { | ||
const handleClick = () => { | ||
emit('update', props.value) | ||
} | ||
return () => h('div', { onClick: handleClick }, String(props.value)) | ||
}, | ||
{ | ||
props: ['value'], | ||
emits: ['update'], | ||
}, | ||
) | ||
|
||
expect(typeof GenericCompWithEmits).toBe('object') | ||
|
||
const root = nodeOps.createElement('div') | ||
render(h(GenericCompWithEmits, { value: 'test' }), root) | ||
expect(serializeInner(root)).toBe(`<div>test</div>`) | ||
}) | ||
|
||
test('should maintain backward compatibility with non-generic functions', () => { | ||
const RegularComp = defineComponent( | ||
(props: { message: string }) => { | ||
return () => h('div', props.message) | ||
}, | ||
{ props: ['message'] }, | ||
) | ||
|
||
expect(typeof RegularComp).toBe('object') | ||
|
||
const root = nodeOps.createElement('div') | ||
render(h(RegularComp, { message: 'hello' }), root) | ||
expect(serializeInner(root)).toBe(`<div>hello</div>`) | ||
}) | ||
|
||
test('should work with union types in generics', () => { | ||
const UnionGenericComp = defineComponent( | ||
<T extends 'small' | 'medium' | 'large'>(props: { size: T }) => { | ||
return () => h('div', props.size) | ||
}, | ||
{ props: ['size'] }, | ||
) | ||
|
||
expect(typeof UnionGenericComp).toBe('object') | ||
|
||
const root1 = nodeOps.createElement('div') | ||
render(h(UnionGenericComp, { size: 'small' }), root1) | ||
expect(serializeInner(root1)).toBe(`<div>small</div>`) | ||
|
||
const root2 = nodeOps.createElement('div') | ||
render(h(UnionGenericComp, { size: 'large' }), root2) | ||
expect(serializeInner(root2)).toBe(`<div>large</div>`) | ||
}) | ||
|
||
test('should work with array generics', () => { | ||
const ArrayGenericComp = defineComponent( | ||
<T>(props: { items: T[]; selectedItem: T }) => { | ||
return () => h('div', `${props.items.length}-${props.selectedItem}`) | ||
}, | ||
{ props: ['items', 'selectedItem'] }, | ||
) | ||
|
||
expect(typeof ArrayGenericComp).toBe('object') | ||
|
||
const root1 = nodeOps.createElement('div') | ||
render( | ||
h(ArrayGenericComp, { | ||
items: ['a', 'b', 'c'], | ||
selectedItem: 'a', | ||
}), | ||
root1, | ||
) | ||
expect(serializeInner(root1)).toBe(`<div>3-a</div>`) | ||
|
||
const root2 = nodeOps.createElement('div') | ||
render( | ||
h(ArrayGenericComp, { | ||
items: [1, 2, 3], | ||
selectedItem: 1, | ||
}), | ||
root2, | ||
) | ||
expect(serializeInner(root2)).toBe(`<div>3-1</div>`) | ||
}) | ||
}) |
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
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.
Uh oh!
There was an error while loading. Please reload this page.