diff --git a/packages/runtime-core/src/apiDefineComponent.ts b/packages/runtime-core/src/apiDefineComponent.ts index 4e40b14d..83446346 100644 --- a/packages/runtime-core/src/apiDefineComponent.ts +++ b/packages/runtime-core/src/apiDefineComponent.ts @@ -41,7 +41,11 @@ export type DefineComponent< E extends EmitsOptions = {}, EE extends string = string, PP = PublicProps, - Props = Readonly> & + Props = Readonly< + PropsOrPropOptions extends ComponentPropsOptions + ? ExtractPropTypes + : PropsOrPropOptions + > & ({} extends E ? {} : EmitsToProps), Defaults = ExtractDefaultPropTypes > = ComponentPublicInstanceConstructor< diff --git a/test-dts/defineComponent.test-d.tsx b/test-dts/defineComponent.test-d.tsx index 8604f8e9..ac7d28ac 100644 --- a/test-dts/defineComponent.test-d.tsx +++ b/test-dts/defineComponent.test-d.tsx @@ -333,35 +333,31 @@ describe('with object props', () => { }) }) -// describe('type inference w/ optional props declaration', () => { -// const MyComponent = defineComponent({ -// setup(_props: { msg: string }) { -// return { -// a: 1 -// } -// }, -// render() { -// expectType(this.$props.msg) -// // props should be readonly -// expectError((this.$props.msg = 'foo')) -// // should not expose on `this` -// expectError(this.msg) -// expectType(this.a) -// return null -// } -// }) +describe('type inference w/ optional props declaration', () => { + const MyComponent = defineComponent<{ a: string[]; msg: string }>({ + setup(props) { + expectType(props.msg) + expectType(props.a) + return { + b: 1 + } + } + }) -// expectType() -// expectError() -// expectError() -// }) + expectType() + // @ts-expect-error + expectError() + // @ts-expect-error + expectError() +}) -// describe('type inference w/ direct setup function', () => { -// const MyComponent = defineComponent((_props: { msg: string }) => {}) -// expectType() -// expectError() -// expectError() -// }) +describe('type inference w/ direct setup function', () => { + const MyComponent = defineComponent((_props: { msg: string }) => {}) + expectType() + // @ts-expect-error + expectError() + expectError() +}) describe('type inference w/ array props declaration', () => { const MyComponent = defineComponent({