Skip to content

Commit

Permalink
fix(types): component type check when props is an empty object
Browse files Browse the repository at this point in the history
  • Loading branch information
rudyxu1102 committed Sep 4, 2023
1 parent 2ffe3d5 commit 416045d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
30 changes: 25 additions & 5 deletions packages/dts-test/defineComponent.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,26 @@ describe('type inference w/ options API', () => {
})
})

// #4051
describe('type inference w/ empty prop object', () => {
const MyComponent = defineComponent({
props: {},
setup(props) {
return {}
},
render() {}
})
expectType<JSX.Element>(<MyComponent />)
// AllowedComponentProps
expectType<JSX.Element>(<MyComponent class={'foo'} />)
// ComponentCustomProps
expectType<JSX.Element>(<MyComponent custom={1} />)
// VNodeProps
expectType<JSX.Element>(<MyComponent key="1" />)
// @ts-expect-error
expectError(<MyComponent other="other" />)
})

describe('with mixins', () => {
const MixinA = defineComponent({
emits: ['bar'],
Expand Down Expand Up @@ -1043,7 +1063,7 @@ describe('inject', () => {
expectType<unknown>(this.foo)
expectType<unknown>(this.bar)
// @ts-expect-error
this.foobar = 1
expectError((this.foobar = 1))
}
})

Expand All @@ -1055,7 +1075,7 @@ describe('inject', () => {
expectType<unknown>(this.foo)
expectType<unknown>(this.bar)
// @ts-expect-error
this.foobar = 1
expectError((this.foobar = 1))
}
})

Expand All @@ -1075,7 +1095,7 @@ describe('inject', () => {
expectType<unknown>(this.foo)
expectType<unknown>(this.bar)
// @ts-expect-error
this.foobar = 1
expectError((this.foobar = 1))
}
})

Expand All @@ -1084,9 +1104,9 @@ describe('inject', () => {
props: ['a', 'b'],
created() {
// @ts-expect-error
this.foo = 1
expectError((this.foo = 1))
// @ts-expect-error
this.bar = 1
expectError((this.bar = 1))
}
})
})
Expand Down
6 changes: 3 additions & 3 deletions packages/runtime-core/src/apiDefineComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,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<ComponentPropsOptions>,
RawBindings,
D,
PropsOptions extends Readonly<ComponentPropsOptions> = {},
RawBindings = {},
D = {},
C extends ComputedOptions = {},
M extends MethodOptions = {},
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
Expand Down

0 comments on commit 416045d

Please sign in to comment.