fix(types): accept generic Component type in h()

fix #922
This commit is contained in:
Evan You 2020-04-04 13:29:29 -04:00
parent 5dcc645fc0
commit c1d5928f3b
2 changed files with 19 additions and 24 deletions

View File

@ -10,14 +10,8 @@ import { Teleport, TeleportProps } from './components/Teleport'
import { Suspense, SuspenseProps } from './components/Suspense' import { Suspense, SuspenseProps } from './components/Suspense'
import { isObject, isArray } from '@vue/shared' import { isObject, isArray } from '@vue/shared'
import { RawSlots } from './componentSlots' import { RawSlots } from './componentSlots'
import { FunctionalComponent } from './component' import { FunctionalComponent, Component } from './component'
import { import { ComponentOptions } from './componentOptions'
ComponentOptionsWithoutProps,
ComponentOptionsWithArrayProps,
ComponentOptionsWithObjectProps,
ComponentOptions
} from './componentOptions'
import { ExtractPropTypes } from './componentProps'
// `h` is a more user-friendly version of `createVNode` that allows omitting the // `h` is a more user-friendly version of `createVNode` that allows omitting the
// props when possible. It is intended for manually written render functions. // props when possible. It is intended for manually written render functions.
@ -108,27 +102,19 @@ export function h(
): VNode ): VNode
// functional component // functional component
export function h(type: FunctionalComponent, children?: RawChildren): VNode
export function h<P>( export function h<P>(
type: FunctionalComponent<P>, type: FunctionalComponent<P>,
props?: (RawProps & P) | ({} extends P ? null : never), props?: (RawProps & P) | ({} extends P ? null : never),
children?: RawChildren | RawSlots children?: RawChildren | RawSlots
): VNode ): VNode
// stateful component // catch-all for generic component types
export function h(type: ComponentOptions, children?: RawChildren): VNode export function h(type: Component, children?: RawChildren): VNode
export function h( export function h(
type: ComponentOptionsWithoutProps | ComponentOptionsWithArrayProps, type: ComponentOptions | FunctionalComponent<{}>,
props?: RawProps | null, props?: RawProps | null,
children?: RawChildren | RawSlots children?: RawChildren | RawSlots
): VNode ): VNode
export function h<O>(
type: ComponentOptionsWithObjectProps<O>,
props?:
| (RawProps & ExtractPropTypes<O>)
| ({} extends ExtractPropTypes<O> ? null : never),
children?: RawChildren | RawSlots
): VNode
// fake constructor type returned by `defineComponent` or class component // fake constructor type returned by `defineComponent` or class component
export function h(type: Constructor, children?: RawChildren): VNode export function h(type: Constructor, children?: RawChildren): VNode

View File

@ -6,7 +6,8 @@ import {
ref, ref,
Fragment, Fragment,
Teleport, Teleport,
Suspense Suspense,
Component
} from './index' } from './index'
describe('h inference w/ element', () => { describe('h inference w/ element', () => {
@ -58,17 +59,15 @@ describe('h inference w/ functional component', () => {
expectError(h(Func, { bar: 123 })) expectError(h(Func, { bar: 123 }))
}) })
describe('h inference w/ plain object component', () => { describe('h support w/ plain object component', () => {
const Foo = { const Foo = {
props: { props: {
foo: String foo: String
} }
} }
h(Foo, { foo: 'ok' }) h(Foo, { foo: 'ok' })
h(Foo, { foo: 'ok', class: 'extra' }) h(Foo, { foo: 'ok', class: 'extra' })
// should fail on wrong type // no inference in this case
expectError(h(Foo, { foo: 1 }))
}) })
describe('h inference w/ defineComponent', () => { describe('h inference w/ defineComponent', () => {
@ -122,3 +121,13 @@ describe('h inference w/ defineComponent + direct function', () => {
// should fail on wrong type // should fail on wrong type
expectError(h(Foo, { bar: 1, foo: 1 })) expectError(h(Foo, { bar: 1, foo: 1 }))
}) })
// #922
describe('h support for generic component type', () => {
function foo(bar: Component) {
h(bar)
h(bar, 'hello')
h(bar, { id: 'ok' }, 'hello')
}
foo({})
})