From d8b2b9eb9c5836bef7650f46c0f661bc3c1b0cd3 Mon Sep 17 00:00:00 2001 From: Jooger Date: Mon, 14 Oct 2019 23:02:49 +0800 Subject: [PATCH] types(ref): improve UnwrapRef types (#266) --- packages/reactivity/src/computed.ts | 2 +- packages/reactivity/src/ref.ts | 21 ++++++++++++++------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/packages/reactivity/src/computed.ts b/packages/reactivity/src/computed.ts index dcc1d4a4..3dc478f6 100644 --- a/packages/reactivity/src/computed.ts +++ b/packages/reactivity/src/computed.ts @@ -7,7 +7,7 @@ export interface ComputedRef extends WritableComputedRef { } export interface WritableComputedRef extends Ref { - readonly effect: ReactiveEffect + readonly effect: ReactiveEffect } export interface WritableComputedOptions { diff --git a/packages/reactivity/src/ref.ts b/packages/reactivity/src/ref.ts index 234a638b..07953e0b 100644 --- a/packages/reactivity/src/ref.ts +++ b/packages/reactivity/src/ref.ts @@ -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 = { + cRef: T extends ComputedRef ? UnwrapRef : T + wcRef: T extends WritableComputedRef ? UnwrapRef : T ref: T extends Ref ? UnwrapRef : T array: T extends Array ? Array> : T object: { [K in keyof T]: UnwrapRef } stop: T -}[T extends Ref - ? 'ref' - : T extends Array - ? 'array' - : T extends BailTypes - ? 'stop' // bail out on types that shouldn't be unwrapped - : T extends object ? 'object' : 'stop'] +}[T extends ComputedRef + ? 'cRef' + : T extends WritableComputedRef + ? 'wcRef' + : T extends Ref + ? 'ref' + : T extends Array + ? '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 extends Ref ? T : UnwrapRef