Skip to content

Commit fca20a3

Browse files
committed
fix(types): fix defineComponent props inference when setup() has explicit annotation
close #11803
1 parent 98864a7 commit fca20a3

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

packages-private/dts-test/defineComponent.test-d.tsx

+12
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,18 @@ describe('emits', () => {
10411041
},
10421042
})
10431043

1044+
// #11803 manual props annotation in setup()
1045+
const Hello = defineComponent({
1046+
name: 'HelloWorld',
1047+
inheritAttrs: false,
1048+
props: { foo: String },
1049+
emits: {
1050+
customClick: (args: string) => typeof args === 'string',
1051+
},
1052+
setup(props: { foo?: string }) {},
1053+
})
1054+
;<Hello onCustomClick={() => {}} />
1055+
10441056
// without emits
10451057
defineComponent({
10461058
setup(props, { emit }) {

packages/runtime-core/src/apiDefineComponent.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ export type DefineSetupFnComponent<
134134
S
135135
>
136136

137+
type ToResolvedProps<Props, Emits extends EmitsOptions> = Readonly<Props> &
138+
Readonly<EmitsToProps<Emits>>
139+
137140
// defineComponent is a utility that is primarily used for type inference
138141
// when declaring components. Type inference is provided in the component
139142
// options (provided as the argument). The returned value has artificial types
@@ -210,8 +213,6 @@ export function defineComponent<
210213
: ExtractPropTypes<RuntimePropsOptions>
211214
: { [key in RuntimePropsKeys]?: any }
212215
: TypeProps,
213-
ResolvedProps = Readonly<InferredProps> &
214-
Readonly<EmitsToProps<ResolvedEmits>>,
215216
TypeRefs extends Record<string, unknown> = {},
216217
>(
217218
options: {
@@ -229,7 +230,7 @@ export function defineComponent<
229230
*/
230231
__typeRefs?: TypeRefs
231232
} & ComponentOptionsBase<
232-
ResolvedProps,
233+
ToResolvedProps<InferredProps, ResolvedEmits>,
233234
SetupBindings,
234235
Data,
235236
Computed,
@@ -249,7 +250,7 @@ export function defineComponent<
249250
> &
250251
ThisType<
251252
CreateComponentPublicInstanceWithMixins<
252-
ResolvedProps,
253+
ToResolvedProps<InferredProps, ResolvedEmits>,
253254
SetupBindings,
254255
Data,
255256
Computed,
@@ -278,7 +279,7 @@ export function defineComponent<
278279
ResolvedEmits,
279280
RuntimeEmitsKeys,
280281
PublicProps,
281-
ResolvedProps,
282+
ToResolvedProps<InferredProps, ResolvedEmits>,
282283
ExtractDefaultPropTypes<RuntimePropsOptions>,
283284
Slots,
284285
LocalComponents,

0 commit comments

Comments
 (0)