fix(types): fix defineComponent props inference when setup() has explicit annotation

close #11803
This commit is contained in:
Evan You 2024-09-05 16:44:25 +08:00
parent 98864a7ef5
commit fca20a39aa
No known key found for this signature in database
GPG Key ID: 00E9AB7A6704CE0A
2 changed files with 18 additions and 5 deletions

View File

@ -1041,6 +1041,18 @@ describe('emits', () => {
}, },
}) })
// #11803 manual props annotation in setup()
const Hello = defineComponent({
name: 'HelloWorld',
inheritAttrs: false,
props: { foo: String },
emits: {
customClick: (args: string) => typeof args === 'string',
},
setup(props: { foo?: string }) {},
})
;<Hello onCustomClick={() => {}} />
// without emits // without emits
defineComponent({ defineComponent({
setup(props, { emit }) { setup(props, { emit }) {

View File

@ -134,6 +134,9 @@ export type DefineSetupFnComponent<
S S
> >
type ToResolvedProps<Props, Emits extends EmitsOptions> = Readonly<Props> &
Readonly<EmitsToProps<Emits>>
// defineComponent is a utility that is primarily used for type inference // defineComponent is a utility that is primarily used for type inference
// when declaring components. Type inference is provided in the component // when declaring components. Type inference is provided in the component
// options (provided as the argument). The returned value has artificial types // options (provided as the argument). The returned value has artificial types
@ -210,8 +213,6 @@ export function defineComponent<
: ExtractPropTypes<RuntimePropsOptions> : ExtractPropTypes<RuntimePropsOptions>
: { [key in RuntimePropsKeys]?: any } : { [key in RuntimePropsKeys]?: any }
: TypeProps, : TypeProps,
ResolvedProps = Readonly<InferredProps> &
Readonly<EmitsToProps<ResolvedEmits>>,
TypeRefs extends Record<string, unknown> = {}, TypeRefs extends Record<string, unknown> = {},
>( >(
options: { options: {
@ -229,7 +230,7 @@ export function defineComponent<
*/ */
__typeRefs?: TypeRefs __typeRefs?: TypeRefs
} & ComponentOptionsBase< } & ComponentOptionsBase<
ResolvedProps, ToResolvedProps<InferredProps, ResolvedEmits>,
SetupBindings, SetupBindings,
Data, Data,
Computed, Computed,
@ -249,7 +250,7 @@ export function defineComponent<
> & > &
ThisType< ThisType<
CreateComponentPublicInstanceWithMixins< CreateComponentPublicInstanceWithMixins<
ResolvedProps, ToResolvedProps<InferredProps, ResolvedEmits>,
SetupBindings, SetupBindings,
Data, Data,
Computed, Computed,
@ -278,7 +279,7 @@ export function defineComponent<
ResolvedEmits, ResolvedEmits,
RuntimeEmitsKeys, RuntimeEmitsKeys,
PublicProps, PublicProps,
ResolvedProps, ToResolvedProps<InferredProps, ResolvedEmits>,
ExtractDefaultPropTypes<RuntimePropsOptions>, ExtractDefaultPropTypes<RuntimePropsOptions>,
Slots, Slots,
LocalComponents, LocalComponents,