Skip to content

Commit 559600f

Browse files
committed
feat: support functional components in defineComponent
close #12619
1 parent 9d12106 commit 559600f

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

types/test/v3/define-component-test.tsx

+36
Original file line numberDiff line numberDiff line change
@@ -1079,3 +1079,39 @@ export default {
10791079
}
10801080
})
10811081
}
1082+
1083+
describe('functional w/ array props', () => {
1084+
const Foo = defineComponent({
1085+
functional: true,
1086+
props: ['foo'],
1087+
render(h, ctx) {
1088+
ctx.props.foo
1089+
// @ts-expect-error
1090+
ctx.props.bar
1091+
}
1092+
})
1093+
1094+
;<Foo foo="hi" />
1095+
// @ts-expect-error
1096+
;<Foo bar={123} />
1097+
})
1098+
1099+
describe('functional w/ object props', () => {
1100+
const Foo = defineComponent({
1101+
functional: true,
1102+
props: {
1103+
foo: String
1104+
},
1105+
render(h, ctx) {
1106+
ctx.props.foo
1107+
// @ts-expect-error
1108+
ctx.props.bar
1109+
}
1110+
})
1111+
1112+
;<Foo foo="hi" />
1113+
// @ts-expect-error
1114+
;<Foo foo={123} />
1115+
// @ts-expect-error
1116+
;<Foo bar={123} />
1117+
})

types/v3-define-component.d.ts

+25
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
} from './v3-component-public-instance'
1919
import { Data, HasDefined } from './common'
2020
import { EmitsOptions } from './v3-setup-context'
21+
import { CreateElement, RenderContext } from './umd'
2122

2223
type DefineComponent<
2324
PropsOrPropOptions = {},
@@ -66,6 +67,30 @@ type DefineComponent<
6667
props: PropsOrPropOptions
6768
}
6869

70+
/**
71+
* overload 0.0: functional component with array props
72+
*/
73+
export function defineComponent<
74+
PropNames extends string,
75+
Props = Readonly<{ [key in PropNames]?: any }>
76+
>(options: {
77+
functional: true
78+
props?: PropNames[]
79+
render?: (h: CreateElement, context: RenderContext<Props>) => any
80+
}): DefineComponent<Props>
81+
82+
/**
83+
* overload 0.1: functional component with object props
84+
*/
85+
export function defineComponent<
86+
PropsOptions extends ComponentPropsOptions = ComponentPropsOptions,
87+
Props = ExtractPropTypes<PropsOptions>
88+
>(options: {
89+
functional: true
90+
props?: PropsOptions
91+
render?: (h: CreateElement, context: RenderContext<Props>) => any
92+
}): DefineComponent<PropsOptions>
93+
6994
/**
7095
* overload 1: object format with no props
7196
*/

0 commit comments

Comments
 (0)