]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(types): update setup binding unwrap types for 6b10f0c
authorEvan You <yyx990803@gmail.com>
Wed, 19 Feb 2020 17:29:01 +0000 (18:29 +0100)
committerEvan You <yyx990803@gmail.com>
Wed, 19 Feb 2020 17:29:18 +0000 (18:29 +0100)
close #738

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

index 4582496e442fa041b6ec2f34546f67dc5506348e..9e4511105236c5317f55582b4f19867657e5c4c8 100644 (file)
@@ -8,7 +8,13 @@ import {
   ComputedOptions,
   MethodOptions
 } from './apiOptions'
-import { UnwrapRef, ReactiveEffect, isRef, isReactive } from '@vue/reactivity'
+import {
+  ReactiveEffect,
+  isRef,
+  isReactive,
+  Ref,
+  ComputedRef
+} from '@vue/reactivity'
 import { warn } from './warning'
 import { Slots } from './componentSlots'
 import {
@@ -19,9 +25,9 @@ import {
 // public properties exposed on the proxy, which is used as the render context
 // in templates (as `this` in the render option)
 export type ComponentPublicInstance<
-  P = {},
-  B = {},
-  D = {},
+  P = {}, // props type extracted from props option
+  B = {}, // raw bindings returned from setup()
+  D = {}, // return from data()
   C extends ComputedOptions = {},
   M extends MethodOptions = {},
   PublicProps = P
@@ -40,11 +46,17 @@ export type ComponentPublicInstance<
   $nextTick: typeof nextTick
   $watch: typeof instanceWatch
 } & P &
-  UnwrapRef<B> &
+  UnwrapSetupBindings<B> &
   D &
   ExtractComputedReturns<C> &
   M
 
+type UnwrapSetupBindings<B> = { [K in keyof B]: UnwrapBinding<B[K]> }
+
+type UnwrapBinding<B> = B extends ComputedRef<any>
+  ? B extends ComputedRef<infer V> ? V : B
+  : B extends Ref<infer V> ? V : B
+
 const publicPropertiesMap: Record<
   string,
   (i: ComponentInternalInstance) => any
index 417df9d1a5a89ffd4923c907a3099ba37a426822..a9e7e427ee602eb42c9cc6c8754f3c0303392c03 100644 (file)
@@ -1,5 +1,13 @@
 import { expectError, expectType } from 'tsd'
-import { describe, defineComponent, PropType, ref, createApp } from './index'
+import {
+  describe,
+  defineComponent,
+  PropType,
+  ref,
+  Ref,
+  reactive,
+  createApp
+} from './index'
 
 describe('with object props', () => {
   interface ExpectedProps {
@@ -57,11 +65,14 @@ describe('with object props', () => {
       // setup context
       return {
         c: ref(1),
-        d: {
+        d: reactive({
           e: ref('hi')
-        },
-        f: {
+        }),
+        f: reactive({
           g: ref('hello' as GT)
+        }),
+        h: {
+          i: ref('hi')
         }
       }
     },
@@ -95,6 +106,9 @@ describe('with object props', () => {
       expectType<string>(this.d.e)
       expectType<GT>(this.f.g)
 
+      // should not unwrap refs nested under non-reactive objects
+      expectType<Ref<string>>(this.h.i)
+
       // setup context properties should be mutable
       this.c = 2