From 44448fdd223f53a73553f4197f4c4fabf6f9a27b Mon Sep 17 00:00:00 2001 From: Pick Date: Sat, 15 Aug 2020 05:37:36 +0800 Subject: [PATCH] types(reactivity): add dts for proxyRefs & improve typings (#1786) --- packages/reactivity/src/baseHandlers.ts | 8 +++---- packages/reactivity/src/reactive.ts | 2 +- test-dts/ref.test-d.ts | 28 ++++++++++++++++++++++++- 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/packages/reactivity/src/baseHandlers.ts b/packages/reactivity/src/baseHandlers.ts index 6c0a1a44..11a7b1cc 100644 --- a/packages/reactivity/src/baseHandlers.ts +++ b/packages/reactivity/src/baseHandlers.ts @@ -1,4 +1,4 @@ -import { reactive, readonly, toRaw, ReactiveFlags } from './reactive' +import { reactive, readonly, toRaw, ReactiveFlags, Target } from './reactive' import { TrackOpTypes, TriggerOpTypes } from './operations' import { track, trigger, ITERATE_KEY } from './effect' import { @@ -41,7 +41,7 @@ const arrayInstrumentations: Record = {} }) function createGetter(isReadonly = false, shallow = false) { - return function get(target: object, key: string | symbol, receiver: object) { + return function get(target: Target, key: string | symbol, receiver: object) { if (key === ReactiveFlags.IS_REACTIVE) { return !isReadonly } else if (key === ReactiveFlags.IS_READONLY) { @@ -50,8 +50,8 @@ function createGetter(isReadonly = false, shallow = false) { key === ReactiveFlags.RAW && receiver === (isReadonly - ? (target as any)[ReactiveFlags.READONLY] - : (target as any)[ReactiveFlags.REACTIVE]) + ? target[ReactiveFlags.READONLY] + : target[ReactiveFlags.REACTIVE]) ) { return target } diff --git a/packages/reactivity/src/reactive.ts b/packages/reactivity/src/reactive.ts index 4dea9e00..7541886c 100644 --- a/packages/reactivity/src/reactive.ts +++ b/packages/reactivity/src/reactive.ts @@ -21,7 +21,7 @@ export const enum ReactiveFlags { READONLY = '__v_readonly' } -interface Target { +export interface Target { [ReactiveFlags.SKIP]?: boolean [ReactiveFlags.IS_REACTIVE]?: boolean [ReactiveFlags.IS_READONLY]?: boolean diff --git a/test-dts/ref.test-d.ts b/test-dts/ref.test-d.ts index cafe2143..6430b15c 100644 --- a/test-dts/ref.test-d.ts +++ b/test-dts/ref.test-d.ts @@ -1,4 +1,12 @@ -import { Ref, ref, isRef, unref, reactive, expectType } from './index' +import { + Ref, + ref, + isRef, + unref, + reactive, + expectType, + proxyRefs +} from './index' function plainType(arg: number | Ref) { // ref coercing @@ -111,3 +119,21 @@ const state = reactive({ }) expectType(state.foo.label) + +// proxyRefs: should return `reactive` directly +const r1 = reactive({ + k: 'v' +}) +const p1 = proxyRefs(r1) +expectType(p1) + +// proxyRefs: `ShallowUnwrapRef` +const r2 = { + a: ref(1), + obj: { + k: ref('foo') + } +} +const p2 = proxyRefs(r2) +expectType(p2.a) +expectType>(p2.obj.k)