Przeglądaj źródła

fix(types): unwrap refs on public instance data (#3319)

fix #3315
HcySunYang 5 lat temu
rodzic
commit
2b588cf1bc

+ 2 - 1
packages/reactivity/src/index.ts

@@ -25,7 +25,8 @@ export {
   markRaw,
   markRaw,
   toRaw,
   toRaw,
   ReactiveFlags,
   ReactiveFlags,
-  DeepReadonly
+  DeepReadonly,
+  UnwrapNestedRefs
 } from './reactive'
 } from './reactive'
 export {
 export {
   computed,
   computed,

+ 1 - 1
packages/reactivity/src/reactive.ts

@@ -57,7 +57,7 @@ function getTargetType(value: Target) {
 }
 }
 
 
 // only unwrap nested ref
 // only unwrap nested ref
-type UnwrapNestedRefs<T> = T extends Ref ? T : UnwrapRef<T>
+export type UnwrapNestedRefs<T> = T extends Ref ? T : UnwrapRef<T>
 
 
 /**
 /**
  * Creates a reactive copy of the original object.
  * Creates a reactive copy of the original object.

+ 3 - 2
packages/runtime-core/src/componentPublicInstance.ts

@@ -20,7 +20,8 @@ import {
   ReactiveFlags,
   ReactiveFlags,
   track,
   track,
   TrackOpTypes,
   TrackOpTypes,
-  ShallowUnwrapRef
+  ShallowUnwrapRef,
+  UnwrapNestedRefs
 } from '@vue/reactivity'
 } from '@vue/reactivity'
 import {
 import {
   ExtractComputedReturns,
   ExtractComputedReturns,
@@ -195,7 +196,7 @@ export type ComponentPublicInstance<
   ): WatchStopHandle
   ): WatchStopHandle
 } & P &
 } & P &
   ShallowUnwrapRef<B> &
   ShallowUnwrapRef<B> &
-  D &
+  UnwrapNestedRefs<D> &
   ExtractComputedReturns<C> &
   ExtractComputedReturns<C> &
   M &
   M &
   ComponentCustomProperties
   ComponentCustomProperties

+ 3 - 1
test-dts/defineComponent.test-d.tsx

@@ -379,7 +379,8 @@ describe('type inference w/ options API', () => {
       // here in data() - somehow that would mess up the inference
       // here in data() - somehow that would mess up the inference
       expectType<number | undefined>(this.a)
       expectType<number | undefined>(this.a)
       return {
       return {
-        c: this.a || 123
+        c: this.a || 123,
+        someRef: ref(0)
       }
       }
     },
     },
     computed: {
     computed: {
@@ -418,6 +419,7 @@ describe('type inference w/ options API', () => {
       expectType<number>(this.d)
       expectType<number>(this.d)
       // computed get/set
       // computed get/set
       expectType<number>(this.e)
       expectType<number>(this.e)
+      expectType<number>(this.someRef)
     },
     },
     methods: {
     methods: {
       doSomething() {
       doSomething() {