Skip to content

Commit

Permalink
fix(types): fix defineComponent props inference when setup() has expl…
Browse files Browse the repository at this point in the history
…icit annotation

close #11803
  • Loading branch information
yyx990803 committed Sep 5, 2024
1 parent 98864a7 commit fca20a3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
12 changes: 12 additions & 0 deletions packages-private/dts-test/defineComponent.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
defineComponent({
setup(props, { emit }) {
Expand Down
11 changes: 6 additions & 5 deletions packages/runtime-core/src/apiDefineComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ export type DefineSetupFnComponent<
S
>

type ToResolvedProps<Props, Emits extends EmitsOptions> = Readonly<Props> &
Readonly<EmitsToProps<Emits>>

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

0 comments on commit fca20a3

Please sign in to comment.