Skip to content

Commit

Permalink
feat: support functional components in defineComponent
Browse files Browse the repository at this point in the history
close #12619
  • Loading branch information
yyx990803 committed Jul 8, 2022
1 parent 9d12106 commit 559600f
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
36 changes: 36 additions & 0 deletions types/test/v3/define-component-test.tsx
Expand Up @@ -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} />
})
25 changes: 25 additions & 0 deletions types/v3-define-component.d.ts
Expand Up @@ -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 = {},
Expand Down Expand Up @@ -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
*/
Expand Down

1 comment on commit 559600f

@PiXVE89

This comment was marked as spam.

Please sign in to comment.