]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(types): fix instance type when props type is incompatible with setup returned...
authorRudy <xuhaodong66@foxmail.com>
Thu, 9 Nov 2023 09:40:36 +0000 (17:40 +0800)
committerGitHub <noreply@github.com>
Thu, 9 Nov 2023 09:40:36 +0000 (17:40 +0800)
close #5885

packages/dts-test/defineComponent.test-d.tsx
packages/runtime-core/src/apiDefineComponent.ts
packages/runtime-core/src/componentPublicInstance.ts

index 7466249e10f7a3e019718e0582b70bc882f79f31..b3f735ddad96504a220ebfe117cddcf317ef6f4a 100644 (file)
@@ -1472,6 +1472,31 @@ describe('slots', () => {
   expectType<Slots | undefined>(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<SizeType>,
+        required: true
+      }
+    },
+    setup(props) {
+      expectType<SizeType>(props.size)
+      return {
+        size: 1
+      }
+    }
+  })
+  type CompInstance = InstanceType<typeof Comp>
+
+  const CompA = {} as CompInstance
+  expectType<ComponentPublicInstance>(CompA)
+  expectType<number>(CompA.size)
+  expectType<SizeType>(CompA.$props.size)
+})
+
 import {
   DefineComponent,
   ComponentOptionsMixin,
index 272bb5487516b1c339c642b8e893a78de0a41b92..092f679e9664fa4e9c4e2a804f16de5f89fe6443 100644 (file)
@@ -70,8 +70,7 @@ export type DefineComponent<
     true,
     {},
     S
-  > &
-    Props
+  >
 > &
   ComponentOptionsBase<
     Props,
index b7ef1e07302d6c55a49ab6adba55302d27515992..6ef0c1b9ff27340e8a0311cfb69fb78b6a2149b8 100644 (file)
@@ -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<P, P, Omit<P, keyof ShallowUnwrapRef<B>>> &
   ShallowUnwrapRef<B> &
   UnwrapNestedRefs<D> &
   ExtractComputedReturns<C> &