From 0e1e8f919e5a74cdaadf9c80ee135088b25e7fa3 Mon Sep 17 00:00:00 2001 From: Rudy Date: Thu, 9 Nov 2023 17:40:36 +0800 Subject: [PATCH] fix(types): fix instance type when props type is incompatible with setup returned type (#7338) close #5885 --- packages/dts-test/defineComponent.test-d.tsx | 25 +++++++++++++++++++ .../runtime-core/src/apiDefineComponent.ts | 3 +-- .../src/componentPublicInstance.ts | 6 ++--- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/packages/dts-test/defineComponent.test-d.tsx b/packages/dts-test/defineComponent.test-d.tsx index 7466249e10f..b3f735ddad9 100644 --- a/packages/dts-test/defineComponent.test-d.tsx +++ b/packages/dts-test/defineComponent.test-d.tsx @@ -1472,6 +1472,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 b7ef1e07302..6ef0c1b9ff2 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< @@ -226,7 +226,7 @@ export type ComponentPublicInstance< : (...args: any) => any, options?: WatchOptions ): WatchStopHandle -} & P & +} & IfAny>> & ShallowUnwrapRef & UnwrapNestedRefs & ExtractComputedReturns &