From: Amour1688 Date: Tue, 21 Sep 2021 16:41:10 +0000 (+0800) Subject: fix(types): incorrect type inference of array (#4578) X-Git-Tag: v3.2.13~7 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=140f08991727d7c15db907eea5a101979fe390b2;p=thirdparty%2Fvuejs%2Fcore.git fix(types): incorrect type inference of array (#4578) --- diff --git a/packages/runtime-core/src/apiDefineComponent.ts b/packages/runtime-core/src/apiDefineComponent.ts index 4e40b14d50..83446346fa 100644 --- a/packages/runtime-core/src/apiDefineComponent.ts +++ b/packages/runtime-core/src/apiDefineComponent.ts @@ -41,7 +41,11 @@ export type DefineComponent< E extends EmitsOptions = {}, EE extends string = string, PP = PublicProps, - Props = Readonly> & + Props = Readonly< + PropsOrPropOptions extends ComponentPropsOptions + ? ExtractPropTypes + : PropsOrPropOptions + > & ({} extends E ? {} : EmitsToProps), Defaults = ExtractDefaultPropTypes > = ComponentPublicInstanceConstructor< diff --git a/test-dts/defineComponent.test-d.tsx b/test-dts/defineComponent.test-d.tsx index 8604f8e9fb..ac7d28acb5 100644 --- a/test-dts/defineComponent.test-d.tsx +++ b/test-dts/defineComponent.test-d.tsx @@ -333,35 +333,31 @@ describe('with object props', () => { }) }) -// describe('type inference w/ optional props declaration', () => { -// const MyComponent = defineComponent({ -// setup(_props: { msg: string }) { -// return { -// a: 1 -// } -// }, -// render() { -// expectType(this.$props.msg) -// // props should be readonly -// expectError((this.$props.msg = 'foo')) -// // should not expose on `this` -// expectError(this.msg) -// expectType(this.a) -// return null -// } -// }) - -// expectType() -// expectError() -// expectError() -// }) - -// describe('type inference w/ direct setup function', () => { -// const MyComponent = defineComponent((_props: { msg: string }) => {}) -// expectType() -// expectError() -// expectError() -// }) +describe('type inference w/ optional props declaration', () => { + const MyComponent = defineComponent<{ a: string[]; msg: string }>({ + setup(props) { + expectType(props.msg) + expectType(props.a) + return { + b: 1 + } + } + }) + + expectType() + // @ts-expect-error + expectError() + // @ts-expect-error + expectError() +}) + +describe('type inference w/ direct setup function', () => { + const MyComponent = defineComponent((_props: { msg: string }) => {}) + expectType() + // @ts-expect-error + expectError() + expectError() +}) describe('type inference w/ array props declaration', () => { const MyComponent = defineComponent({