diff --git a/packages/runtime-core/src/apiDefineComponent.ts b/packages/runtime-core/src/apiDefineComponent.ts index c10ac74e4a9..1f858ccc8fb 100644 --- a/packages/runtime-core/src/apiDefineComponent.ts +++ b/packages/runtime-core/src/apiDefineComponent.ts @@ -170,9 +170,9 @@ export function defineComponent< export function defineComponent< // the Readonly constraint allows TS to treat the type of { required: true } // as constant instead of boolean. - PropsOptions extends Readonly, - RawBindings, - D, + PropsOptions extends Readonly = {}, + RawBindings = {}, + D = {}, C extends ComputedOptions = {}, M extends MethodOptions = {}, Mixin extends ComponentOptionsMixin = ComponentOptionsMixin, diff --git a/test-dts/defineComponent.test-d.tsx b/test-dts/defineComponent.test-d.tsx index 208501e4286..5a660d2a2d6 100644 --- a/test-dts/defineComponent.test-d.tsx +++ b/test-dts/defineComponent.test-d.tsx @@ -482,6 +482,26 @@ describe('type inference w/ options API', () => { }) }) +// #4051 +describe('type inference w/ empty prop object', () => { + const MyComponent = defineComponent({ + props: {}, + setup(props) { + return {} + }, + render() {} + }) + expectType() + // AllowedComponentProps + expectType() + // ComponentCustomProps + expectType() + // VNodeProps + expectType() + // @ts-expect-error + expectError() +}) + describe('with mixins', () => { const MixinA = defineComponent({ emits: ['bar'], @@ -1041,13 +1061,13 @@ describe('inject', () => { }, inject: { foo: 'foo', - bar: 'bar', + bar: 'bar' }, created() { expectType(this.foo) expectType(this.bar) // @ts-expect-error - expectError(this.foobar = 1) + expectError((this.foobar = 1)) } }) @@ -1059,7 +1079,7 @@ describe('inject', () => { expectType(this.foo) expectType(this.bar) // @ts-expect-error - expectError(this.foobar = 1) + expectError((this.foobar = 1)) } }) @@ -1073,13 +1093,13 @@ describe('inject', () => { bar: { from: 'pbar', default: 'bar' - }, + } }, created() { expectType(this.foo) expectType(this.bar) // @ts-expect-error - expectError(this.foobar = 1) + expectError((this.foobar = 1)) } }) @@ -1088,9 +1108,9 @@ describe('inject', () => { props: ['a', 'b'], created() { // @ts-expect-error - expectError(this.foo = 1) + expectError((this.foo = 1)) // @ts-expect-error - expectError(this.bar = 1) + expectError((this.bar = 1)) } }) })