]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
types(ref): improve UnwrapRef types (#266)
authorJooger <iamjooger@gmail.com>
Mon, 14 Oct 2019 15:02:49 +0000 (23:02 +0800)
committerEvan You <yyx990803@gmail.com>
Mon, 14 Oct 2019 15:02:49 +0000 (11:02 -0400)
packages/reactivity/src/computed.ts
packages/reactivity/src/ref.ts

index dcc1d4a42c85baa95af07e875c87ff67eccc4981..3dc478f61b62a847153a10a1ba5f60e8e898ed67 100644 (file)
@@ -7,7 +7,7 @@ export interface ComputedRef<T> extends WritableComputedRef<T> {
 }
 
 export interface WritableComputedRef<T> extends Ref<T> {
-  readonly effect: ReactiveEffect
+  readonly effect: ReactiveEffect<T>
 }
 
 export interface WritableComputedOptions<T> {
index 234a638b7923f187cefcd532d190be637212fa58..07953e0bf5d3b59b5b07b13926fd071c8f939607 100644 (file)
@@ -2,6 +2,7 @@ import { track, trigger } from './effect'
 import { OperationTypes } from './operations'
 import { isObject } from '@vue/shared'
 import { reactive } from './reactive'
+import { ComputedRef, WritableComputedRef } from './computed'
 
 export const refSymbol = Symbol(__DEV__ ? 'refSymbol' : '')
 
@@ -71,17 +72,23 @@ type BailTypes =
 
 // Recursively unwraps nested value bindings.
 export type UnwrapRef<T> = {
+  cRef: T extends ComputedRef<infer V> ? UnwrapRef<V> : T
+  wcRef: T extends WritableComputedRef<infer V> ? UnwrapRef<V> : T
   ref: T extends Ref<infer V> ? UnwrapRef<V> : T
   array: T extends Array<infer V> ? Array<UnwrapRef<V>> : T
   object: { [K in keyof T]: UnwrapRef<T[K]> }
   stop: T
-}[T extends Ref
-  ? 'ref'
-  : T extends Array<any>
-    ? 'array'
-    : T extends BailTypes
-      ? 'stop' // bail out on types that shouldn't be unwrapped
-      : T extends object ? 'object' : 'stop']
+}[T extends ComputedRef<any>
+  ? 'cRef'
+  : T extends WritableComputedRef<any>
+    ? 'wcRef'
+    : T extends Ref
+      ? 'ref'
+      : T extends Array<any>
+        ? 'array'
+        : T extends BailTypes
+          ? 'stop' // bail out on types that shouldn't be unwrapped
+          : T extends object ? 'object' : 'stop']
 
 // only unwrap nested ref
 export type UnwrapNestedRefs<T> = T extends Ref ? T : UnwrapRef<T>