feat: support functional components in defineComponent

close #12619
This commit is contained in:
Evan You 2022-07-08 11:18:36 +08:00
parent 9d12106e21
commit 559600f13d
2 changed files with 61 additions and 0 deletions

View File

@ -1079,3 +1079,39 @@ export default {
}
})
}
describe('functional w/ array props', () => {
const Foo = defineComponent({
functional: true,
props: ['foo'],
render(h, ctx) {
ctx.props.foo
// @ts-expect-error
ctx.props.bar
}
})
;<Foo foo="hi" />
// @ts-expect-error
;<Foo bar={123} />
})
describe('functional w/ object props', () => {
const Foo = defineComponent({
functional: true,
props: {
foo: String
},
render(h, ctx) {
ctx.props.foo
// @ts-expect-error
ctx.props.bar
}
})
;<Foo foo="hi" />
// @ts-expect-error
;<Foo foo={123} />
// @ts-expect-error
;<Foo bar={123} />
})

View File

@ -18,6 +18,7 @@ import {
} from './v3-component-public-instance'
import { Data, HasDefined } from './common'
import { EmitsOptions } from './v3-setup-context'
import { CreateElement, RenderContext } from './umd'
type DefineComponent<
PropsOrPropOptions = {},
@ -66,6 +67,30 @@ type DefineComponent<
props: PropsOrPropOptions
}
/**
* overload 0.0: functional component with array props
*/
export function defineComponent<
PropNames extends string,
Props = Readonly<{ [key in PropNames]?: any }>
>(options: {
functional: true
props?: PropNames[]
render?: (h: CreateElement, context: RenderContext<Props>) => any
}): DefineComponent<Props>
/**
* overload 0.1: functional component with object props
*/
export function defineComponent<
PropsOptions extends ComponentPropsOptions = ComponentPropsOptions,
Props = ExtractPropTypes<PropsOptions>
>(options: {
functional: true
props?: PropsOptions
render?: (h: CreateElement, context: RenderContext<Props>) => any
}): DefineComponent<PropsOptions>
/**
* overload 1: object format with no props
*/