types(ref): improve UnwrapRef types (#266)

This commit is contained in:
Jooger 2019-10-14 23:02:49 +08:00 committed by Evan You
parent d2bcedb213
commit d8b2b9eb9c
2 changed files with 15 additions and 8 deletions

View File

@ -7,7 +7,7 @@ export interface ComputedRef<T> extends WritableComputedRef<T> {
} }
export interface WritableComputedRef<T> extends Ref<T> { export interface WritableComputedRef<T> extends Ref<T> {
readonly effect: ReactiveEffect readonly effect: ReactiveEffect<T>
} }
export interface WritableComputedOptions<T> { export interface WritableComputedOptions<T> {

View File

@ -2,6 +2,7 @@ import { track, trigger } from './effect'
import { OperationTypes } from './operations' import { OperationTypes } from './operations'
import { isObject } from '@vue/shared' import { isObject } from '@vue/shared'
import { reactive } from './reactive' import { reactive } from './reactive'
import { ComputedRef, WritableComputedRef } from './computed'
export const refSymbol = Symbol(__DEV__ ? 'refSymbol' : '') export const refSymbol = Symbol(__DEV__ ? 'refSymbol' : '')
@ -71,11 +72,17 @@ type BailTypes =
// Recursively unwraps nested value bindings. // Recursively unwraps nested value bindings.
export type UnwrapRef<T> = { 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 ref: T extends Ref<infer V> ? UnwrapRef<V> : T
array: T extends Array<infer V> ? Array<UnwrapRef<V>> : T array: T extends Array<infer V> ? Array<UnwrapRef<V>> : T
object: { [K in keyof T]: UnwrapRef<T[K]> } object: { [K in keyof T]: UnwrapRef<T[K]> }
stop: T stop: T
}[T extends Ref }[T extends ComputedRef<any>
? 'cRef'
: T extends WritableComputedRef<any>
? 'wcRef'
: T extends Ref
? 'ref' ? 'ref'
: T extends Array<any> : T extends Array<any>
? 'array' ? 'array'