Skip to content

Commit cefe1f2

Browse files
committed
fix(types): component type check when props is an empty object
1 parent fe77e2b commit cefe1f2

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

packages/runtime-core/src/apiDefineComponent.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,9 @@ export function defineComponent<
170170
export function defineComponent<
171171
// the Readonly constraint allows TS to treat the type of { required: true }
172172
// as constant instead of boolean.
173-
PropsOptions extends Readonly<ComponentPropsOptions>,
174-
RawBindings,
175-
D,
173+
PropsOptions extends Readonly<ComponentPropsOptions> = {},
174+
RawBindings = {},
175+
D = {},
176176
C extends ComputedOptions = {},
177177
M extends MethodOptions = {},
178178
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,

test-dts/defineComponent.test-d.tsx

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,26 @@ describe('type inference w/ options API', () => {
482482
})
483483
})
484484

485+
// #4051
486+
describe('type inference w/ empty prop object', () => {
487+
const MyComponent = defineComponent({
488+
props: {},
489+
setup(props) {
490+
return {}
491+
},
492+
render() {}
493+
})
494+
expectType<JSX.Element>(<MyComponent />)
495+
// AllowedComponentProps
496+
expectType<JSX.Element>(<MyComponent class={'foo'} />)
497+
// ComponentCustomProps
498+
expectType<JSX.Element>(<MyComponent custom={1} />)
499+
// VNodeProps
500+
expectType<JSX.Element>(<MyComponent key="1" />)
501+
// @ts-expect-error
502+
expectError(<MyComponent other="other" />)
503+
})
504+
485505
describe('with mixins', () => {
486506
const MixinA = defineComponent({
487507
emits: ['bar'],
@@ -1041,13 +1061,13 @@ describe('inject', () => {
10411061
},
10421062
inject: {
10431063
foo: 'foo',
1044-
bar: 'bar',
1064+
bar: 'bar'
10451065
},
10461066
created() {
10471067
expectType<unknown>(this.foo)
10481068
expectType<unknown>(this.bar)
10491069
// @ts-expect-error
1050-
expectError(this.foobar = 1)
1070+
expectError((this.foobar = 1))
10511071
}
10521072
})
10531073

@@ -1059,7 +1079,7 @@ describe('inject', () => {
10591079
expectType<unknown>(this.foo)
10601080
expectType<unknown>(this.bar)
10611081
// @ts-expect-error
1062-
expectError(this.foobar = 1)
1082+
expectError((this.foobar = 1))
10631083
}
10641084
})
10651085

@@ -1073,13 +1093,13 @@ describe('inject', () => {
10731093
bar: {
10741094
from: 'pbar',
10751095
default: 'bar'
1076-
},
1096+
}
10771097
},
10781098
created() {
10791099
expectType<unknown>(this.foo)
10801100
expectType<unknown>(this.bar)
10811101
// @ts-expect-error
1082-
expectError(this.foobar = 1)
1102+
expectError((this.foobar = 1))
10831103
}
10841104
})
10851105

@@ -1088,9 +1108,9 @@ describe('inject', () => {
10881108
props: ['a', 'b'],
10891109
created() {
10901110
// @ts-expect-error
1091-
expectError(this.foo = 1)
1111+
expectError((this.foo = 1))
10921112
// @ts-expect-error
1093-
expectError(this.bar = 1)
1113+
expectError((this.bar = 1))
10941114
}
10951115
})
10961116
})

0 commit comments

Comments
 (0)