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

types(runtime-core): Add h overload to support string|Component|DefineComponent types #5432

Merged
merged 4 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/runtime-core/src/h.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,14 @@ export function h<P>(
children?: RawChildren | RawSlots
): VNode

// catch or types
yyx990803 marked this conversation as resolved.
Show resolved Hide resolved
export function h(type: string | Component, children?: RawChildren): VNode
export function h<P>(
type: string | Component<P>,
props?: (RawProps & P) | ({} extends P ? null : never),
children?: RawChildren | RawSlots
): VNode

// Actual implementation
export function h(type: any, propsOrChildren?: any, children?: any): VNode {
const l = arguments.length
Expand Down
17 changes: 16 additions & 1 deletion test-dts/h.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DefineComponent } from '@vue/runtime-core'
import {
describe,
h,
Expand Down Expand Up @@ -149,7 +150,6 @@ describe('h support for generic component type', () => {
function foo(bar: Component) {
h(bar)
h(bar, 'hello')
// @ts-expect-error
h(bar, { id: 'ok' }, 'hello')
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After having a thought on this, I don't think having a Prop type check on Component is good, since if the user is using Component interface is because he does not know the type, therefor type checking is not needed here.

If we want type checking having a Prop is recommended

declare function renderMyComp<P>(p: Component<P>, props: P): void
renderMyComp({} as Component<{ a: string }>, { a: '' })

}
foo({})
Expand Down Expand Up @@ -233,3 +233,18 @@ describe('resolveComponent should work', () => {
message: '1'
})
})

// #5431
describe('h should work with multiple types', () => {
const serializers = {
Paragraph: 'p',
Component: {} as Component,
DefineComponent: {} as DefineComponent
}

const sampleComponent = serializers['' as keyof typeof serializers]

h(sampleComponent)
h(sampleComponent, {})
h(sampleComponent, {}, [])
})