test: test unwrapping computed refs

This commit is contained in:
Evan You
2019-10-14 11:21:09 -04:00
parent d8b2b9eb9c
commit 1c56d1bf19
3 changed files with 29 additions and 17 deletions

View File

@@ -2,7 +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'
import { ComputedRef } from './computed'
export const refSymbol = Symbol(__DEV__ ? 'refSymbol' : '')
@@ -73,22 +73,16 @@ 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 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>
: 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']