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 Feb 23, 2023
1 parent a0e7dc3 commit cfdb517
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 @@ -475,6 +475,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 @@ -1040,7 +1060,7 @@ describe('inject', () => {
expectType<unknown>(this.foo)
expectType<unknown>(this.bar)
// @ts-expect-error
this.foobar = 1
expectError((this.foobar = 1))
}
})

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

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

Expand All @@ -1081,9 +1101,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 @@ -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<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 cfdb517

Please sign in to comment.