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 Dec 27, 2022
1 parent fe77e2b commit cefe1f2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
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
34 changes: 27 additions & 7 deletions test-dts/defineComponent.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<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 @@ -1041,13 +1061,13 @@ describe('inject', () => {
},
inject: {
foo: 'foo',
bar: 'bar',
bar: 'bar'
},
created() {
expectType<unknown>(this.foo)
expectType<unknown>(this.bar)
// @ts-expect-error
expectError(this.foobar = 1)
expectError((this.foobar = 1))
}
})

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

Expand All @@ -1073,13 +1093,13 @@ describe('inject', () => {
bar: {
from: 'pbar',
default: 'bar'
},
}
},
created() {
expectType<unknown>(this.foo)
expectType<unknown>(this.bar)
// @ts-expect-error
expectError(this.foobar = 1)
expectError((this.foobar = 1))
}
})

Expand All @@ -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))
}
})
})
Expand Down

0 comments on commit cefe1f2

Please sign in to comment.