Skip to content

Commit 140f089

Browse files
authored
fix(types): incorrect type inference of array (#4578)
1 parent 58b1fa5 commit 140f089

File tree

2 files changed

+30
-30
lines changed

2 files changed

+30
-30
lines changed

packages/runtime-core/src/apiDefineComponent.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ export type DefineComponent<
4141
E extends EmitsOptions = {},
4242
EE extends string = string,
4343
PP = PublicProps,
44-
Props = Readonly<ExtractPropTypes<PropsOrPropOptions>> &
44+
Props = Readonly<
45+
PropsOrPropOptions extends ComponentPropsOptions
46+
? ExtractPropTypes<PropsOrPropOptions>
47+
: PropsOrPropOptions
48+
> &
4549
({} extends E ? {} : EmitsToProps<E>),
4650
Defaults = ExtractDefaultPropTypes<PropsOrPropOptions>
4751
> = ComponentPublicInstanceConstructor<

test-dts/defineComponent.test-d.tsx

+25-29
Original file line numberDiff line numberDiff line change
@@ -333,35 +333,31 @@ describe('with object props', () => {
333333
})
334334
})
335335

336-
// describe('type inference w/ optional props declaration', () => {
337-
// const MyComponent = defineComponent({
338-
// setup(_props: { msg: string }) {
339-
// return {
340-
// a: 1
341-
// }
342-
// },
343-
// render() {
344-
// expectType<string>(this.$props.msg)
345-
// // props should be readonly
346-
// expectError((this.$props.msg = 'foo'))
347-
// // should not expose on `this`
348-
// expectError(this.msg)
349-
// expectType<number>(this.a)
350-
// return null
351-
// }
352-
// })
353-
354-
// expectType<JSX.Element>(<MyComponent msg="foo" />)
355-
// expectError(<MyComponent />)
356-
// expectError(<MyComponent msg={1} />)
357-
// })
358-
359-
// describe('type inference w/ direct setup function', () => {
360-
// const MyComponent = defineComponent((_props: { msg: string }) => {})
361-
// expectType<JSX.Element>(<MyComponent msg="foo" />)
362-
// expectError(<MyComponent />)
363-
// expectError(<MyComponent msg={1} />)
364-
// })
336+
describe('type inference w/ optional props declaration', () => {
337+
const MyComponent = defineComponent<{ a: string[]; msg: string }>({
338+
setup(props) {
339+
expectType<string>(props.msg)
340+
expectType<string[]>(props.a)
341+
return {
342+
b: 1
343+
}
344+
}
345+
})
346+
347+
expectType<JSX.Element>(<MyComponent msg="1" a={['1']} />)
348+
// @ts-expect-error
349+
expectError(<MyComponent />)
350+
// @ts-expect-error
351+
expectError(<MyComponent msg="1" />)
352+
})
353+
354+
describe('type inference w/ direct setup function', () => {
355+
const MyComponent = defineComponent((_props: { msg: string }) => {})
356+
expectType<JSX.Element>(<MyComponent msg="foo" />)
357+
// @ts-expect-error
358+
expectError(<MyComponent />)
359+
expectError(<MyComponent msg="1" />)
360+
})
365361

366362
describe('type inference w/ array props declaration', () => {
367363
const MyComponent = defineComponent({

0 commit comments

Comments
 (0)