]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
types(reactivity): ref type should not expose _isRef
authorEvan You <yyx990803@gmail.com>
Fri, 8 Nov 2019 18:29:43 +0000 (13:29 -0500)
committerEvan You <yyx990803@gmail.com>
Fri, 8 Nov 2019 18:29:43 +0000 (13:29 -0500)
packages/reactivity/src/computed.ts
packages/reactivity/src/ref.ts

index c80297bb52182c4c57a776a83298dead70080096..2788f89010587bc51e9391bfdfd54d6ee5bfbdb1 100644 (file)
@@ -69,7 +69,7 @@ export function computed<T>(
     set value(newValue: T) {
       setter(newValue)
     }
-  }
+  } as any
 }
 
 function trackChildRun(childRunner: ReactiveEffect) {
index 19c042a0fec46d4be8c7300d714365b3c633193c..b6c7835c4b4d1faadc6e95d47311b6be727addc9 100644 (file)
@@ -5,14 +5,28 @@ import { reactive, isReactive } from './reactive'
 import { ComputedRef } from './computed'
 import { CollectionTypes } from './collectionHandlers'
 
+const isRefSymbol = Symbol()
+
 export interface Ref<T = any> {
-  _isRef: true
+  // This field is necessary to allow TS to differentiate a Ref from a plain
+  // object that happens to have a "value" field.
+  // However, checking a symbol on an arbitrary object is much slower than
+  // checking a plain property, so we use a _isRef plain property for isRef()
+  // check in the actual implementation.
+  // The reason for not just declaring _isRef in the interface is because we
+  // don't want this internal field to leak into userland autocompletion -
+  // a private symbol, on the other hand, achieves just that.
+  [isRefSymbol]: true
   value: UnwrapRef<T>
 }
 
 const convert = <T extends unknown>(val: T): T =>
   isObject(val) ? reactive(val) : val
 
+export function isRef(r: any): r is Ref {
+  return r ? r._isRef === true : false
+}
+
 export function ref<T extends Ref>(raw: T): T
 export function ref<T>(raw: T): Ref<T>
 export function ref<T = any>(): Ref<T>
@@ -37,11 +51,7 @@ export function ref(raw?: unknown) {
       )
     }
   }
-  return r as Ref
-}
-
-export function isRef(r: any): r is Ref {
-  return r ? r._isRef === true : false
+  return r
 }
 
 export function toRefs<T extends object>(
@@ -69,7 +79,7 @@ function toProxyRef<T extends object, K extends keyof T>(
     set value(newVal) {
       object[key] = newVal
     }
-  }
+  } as any
 }
 
 type UnwrapArray<T> = { [P in keyof T]: UnwrapRef<T[P]> }