From ccf6b3bde1c727b023230df0ef81b37004907732 Mon Sep 17 00:00:00 2001 From: rudy-xhd Date: Tue, 13 Dec 2022 23:36:34 +0800 Subject: [PATCH 1/2] fix(types): props type is incompatible with setup returned type --- packages/dts-test/defineComponent.test-d.tsx | 72 ++++++++++++------- .../runtime-core/src/apiDefineComponent.ts | 3 +- .../src/componentPublicInstance.ts | 6 +- 3 files changed, 51 insertions(+), 30 deletions(-) diff --git a/packages/dts-test/defineComponent.test-d.tsx b/packages/dts-test/defineComponent.test-d.tsx index 7466249e10f..0f622b2cb09 100644 --- a/packages/dts-test/defineComponent.test-d.tsx +++ b/packages/dts-test/defineComponent.test-d.tsx @@ -1346,22 +1346,20 @@ describe('function syntax w/ runtime props', () => { (_props: { msg: T }) => { return () => {} }, - { - props: { - msg: String + setup(props) { + expectType(props.size) + return { + size: 1 } } - ) + }) + type CompInstance = InstanceType - // @ts-expect-error string prop names don't match - defineComponent( - (_props: { msg: string }) => { - return () => {} - }, - { - props: ['bar'] - } - ) + const CompA = {} as CompInstance + expectType(CompA) + expectType(CompA.size) + expectType(CompA.$props.size) +}) defineComponent( (_props: { msg: string }) => { @@ -1372,21 +1370,20 @@ describe('function syntax w/ runtime props', () => { // @ts-expect-error prop type mismatch msg: Number } - } - ) - - // @ts-expect-error prop keys don't match - defineComponent( - (_props: { msg: string }, ctx) => { - return () => {} }, - { - props: { - msg: String, - bar: String + setup(props) { + expectType(props.size) + return { + size: 1 } } - ) + }) + type CompInstance = InstanceType + + const CompA = {} as CompInstance + expectType(CompA) + expectType(CompA.size) + expectType(CompA.$props.size) }) // check if defineComponent can be exported @@ -1472,6 +1469,31 @@ describe('slots', () => { expectType(new comp2().$slots) }) +// #5885 +describe('should work when props type is incompatible with setup returned type ', () => { + type SizeType = 'small' | 'big' + const Comp = defineComponent({ + props: { + size: { + type: String as PropType, + required: true + } + }, + setup(props) { + expectType(props.size) + return { + size: 1 + } + } + }) + type CompInstance = InstanceType + + const CompA = {} as CompInstance + expectType(CompA) + expectType(CompA.size) + expectType(CompA.$props.size) +}) + import { DefineComponent, ComponentOptionsMixin, diff --git a/packages/runtime-core/src/apiDefineComponent.ts b/packages/runtime-core/src/apiDefineComponent.ts index 272bb548751..092f679e966 100644 --- a/packages/runtime-core/src/apiDefineComponent.ts +++ b/packages/runtime-core/src/apiDefineComponent.ts @@ -70,8 +70,7 @@ export type DefineComponent< true, {}, S - > & - Props + > > & ComponentOptionsBase< Props, diff --git a/packages/runtime-core/src/componentPublicInstance.ts b/packages/runtime-core/src/componentPublicInstance.ts index dc575aafff9..21365573f11 100644 --- a/packages/runtime-core/src/componentPublicInstance.ts +++ b/packages/runtime-core/src/componentPublicInstance.ts @@ -15,7 +15,8 @@ import { isString, isFunction, UnionToIntersection, - Prettify + Prettify, + IfAny } from '@vue/shared' import { toRaw, @@ -187,7 +188,6 @@ export type CreateComponentPublicInstance< I, S > - // public properties exposed on the proxy, which is used as the render context // in templates (as `this` in the render option) export type ComponentPublicInstance< @@ -228,7 +228,7 @@ export type ComponentPublicInstance< : (...args: any) => any, options?: WatchOptions ): WatchStopHandle -} & P & +} & IfAny>> & ShallowUnwrapRef & UnwrapNestedRefs & ExtractComputedReturns & From d8624a34f9af5150ead9faa8ff13ece0294e2b2e Mon Sep 17 00:00:00 2001 From: rudyxu1102 Date: Tue, 5 Sep 2023 23:30:14 +0800 Subject: [PATCH 2/2] chore: rebase main --- packages/dts-test/defineComponent.test-d.tsx | 47 +++++++++++--------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/packages/dts-test/defineComponent.test-d.tsx b/packages/dts-test/defineComponent.test-d.tsx index 0f622b2cb09..b3f735ddad9 100644 --- a/packages/dts-test/defineComponent.test-d.tsx +++ b/packages/dts-test/defineComponent.test-d.tsx @@ -1346,20 +1346,22 @@ describe('function syntax w/ runtime props', () => { (_props: { msg: T }) => { return () => {} }, - setup(props) { - expectType(props.size) - return { - size: 1 + { + props: { + msg: String } } - }) - type CompInstance = InstanceType + ) - const CompA = {} as CompInstance - expectType(CompA) - expectType(CompA.size) - expectType(CompA.$props.size) -}) + // @ts-expect-error string prop names don't match + defineComponent( + (_props: { msg: string }) => { + return () => {} + }, + { + props: ['bar'] + } + ) defineComponent( (_props: { msg: string }) => { @@ -1370,20 +1372,21 @@ describe('function syntax w/ runtime props', () => { // @ts-expect-error prop type mismatch msg: Number } + } + ) + + // @ts-expect-error prop keys don't match + defineComponent( + (_props: { msg: string }, ctx) => { + return () => {} }, - setup(props) { - expectType(props.size) - return { - size: 1 + { + props: { + msg: String, + bar: String } } - }) - type CompInstance = InstanceType - - const CompA = {} as CompInstance - expectType(CompA) - expectType(CompA.size) - expectType(CompA.$props.size) + ) }) // check if defineComponent can be exported