From bb2b173627cb76234c96e2455bf0e6a72608958b Mon Sep 17 00:00:00 2001 From: Carlos Rodrigues Date: Thu, 7 Nov 2019 11:17:44 +0000 Subject: [PATCH 1/4] fix: improve typescript error messages for ref and simplifying unwrapping refs type --- packages/reactivity/__tests__/ref.spec.ts | 8 +++++++ packages/reactivity/src/ref.ts | 26 +++++++++++------------ 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/packages/reactivity/__tests__/ref.spec.ts b/packages/reactivity/__tests__/ref.spec.ts index 89832ad6367..34a440b1394 100644 --- a/packages/reactivity/__tests__/ref.spec.ts +++ b/packages/reactivity/__tests__/ref.spec.ts @@ -61,6 +61,14 @@ describe('reactivity/ref', () => { expect(dummy1).toBe(3) expect(dummy2).toBe(3) expect(dummy3).toBe(3) + obj.b.c++ + expect(dummy1).toBe(4) + expect(dummy2).toBe(4) + expect(dummy3).toBe(4) + obj.b.d[0]++ + expect(dummy1).toBe(5) + expect(dummy2).toBe(5) + expect(dummy3).toBe(5) }) it('should unwrap nested ref in types', () => { diff --git a/packages/reactivity/src/ref.ts b/packages/reactivity/src/ref.ts index 284bf6c1aa2..77bf76b2fb9 100644 --- a/packages/reactivity/src/ref.ts +++ b/packages/reactivity/src/ref.ts @@ -72,18 +72,16 @@ function toProxyRef( } } -// Recursively unwraps nested value bindings. -export type UnwrapRef = { - cRef: T extends ComputedRef ? UnwrapRef : T - ref: T extends Ref ? UnwrapRef : T - array: T extends Array ? Array> : T - object: { [K in keyof T]: UnwrapRef } -}[T extends ComputedRef - ? 'cRef' - : T extends Ref - ? 'ref' - : T extends Array - ? 'array' +type UnwrapRefPropValue = T extends Array + ? Array> + : T extends ComputedRef + ? UnwrapRef + : T extends Ref + ? UnwrapRef : T extends Function | CollectionTypes - ? 'ref' // bail out on types that shouldn't be unwrapped - : T extends object ? 'object' : 'ref'] + ? T + : T extends object ? { [K in keyof T]: UnwrapRefPropValue } : T + +export type UnwrapRef = T extends Ref + ? { [K in keyof V]: UnwrapRefPropValue } + : { [K in keyof T]: UnwrapRefPropValue } From a857939ce18a8eeb489760e7ee146850f87ed78d Mon Sep 17 00:00:00 2001 From: Carlos Rodrigues Date: Thu, 7 Nov 2019 13:06:33 +0000 Subject: [PATCH 2/4] chore fix tests --- packages/reactivity/src/ref.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/reactivity/src/ref.ts b/packages/reactivity/src/ref.ts index 77bf76b2fb9..45302402d7a 100644 --- a/packages/reactivity/src/ref.ts +++ b/packages/reactivity/src/ref.ts @@ -82,6 +82,8 @@ type UnwrapRefPropValue = T extends Array ? T : T extends object ? { [K in keyof T]: UnwrapRefPropValue } : T -export type UnwrapRef = T extends Ref - ? { [K in keyof V]: UnwrapRefPropValue } - : { [K in keyof T]: UnwrapRefPropValue } +export type UnwrapRef = T extends object + ? T extends Ref + ? { [K in keyof V]: UnwrapRefPropValue } + : { [K in keyof T]: UnwrapRefPropValue } + : T From c8b5884217145aa44368a6bfc17f6c233f57c5a3 Mon Sep 17 00:00:00 2001 From: pikax Date: Thu, 7 Nov 2019 13:30:47 +0000 Subject: [PATCH 3/4] chore: fix the other tests... --- packages/reactivity/src/ref.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/reactivity/src/ref.ts b/packages/reactivity/src/ref.ts index 45302402d7a..b808530883f 100644 --- a/packages/reactivity/src/ref.ts +++ b/packages/reactivity/src/ref.ts @@ -82,8 +82,10 @@ type UnwrapRefPropValue = T extends Array ? T : T extends object ? { [K in keyof T]: UnwrapRefPropValue } : T -export type UnwrapRef = T extends object - ? T extends Ref - ? { [K in keyof V]: UnwrapRefPropValue } - : { [K in keyof T]: UnwrapRefPropValue } - : T +export type UnwrapRef = T extends Function | CollectionTypes + ? T + : T extends object + ? T extends Ref + ? { [K in keyof V]: UnwrapRefPropValue } + : { [K in keyof T]: UnwrapRefPropValue } + : T From 7a7167d3b99be81028c1c71cb4f8143de201351c Mon Sep 17 00:00:00 2001 From: Carlos Rodrigues Date: Thu, 7 Nov 2019 13:48:21 +0000 Subject: [PATCH 4/4] rollback to prev UnwrapRef --- packages/reactivity/src/ref.ts | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/packages/reactivity/src/ref.ts b/packages/reactivity/src/ref.ts index b808530883f..284bf6c1aa2 100644 --- a/packages/reactivity/src/ref.ts +++ b/packages/reactivity/src/ref.ts @@ -72,20 +72,18 @@ function toProxyRef( } } -type UnwrapRefPropValue = T extends Array - ? Array> - : T extends ComputedRef - ? UnwrapRef - : T extends Ref - ? UnwrapRef +// Recursively unwraps nested value bindings. +export type UnwrapRef = { + cRef: T extends ComputedRef ? UnwrapRef : T + ref: T extends Ref ? UnwrapRef : T + array: T extends Array ? Array> : T + object: { [K in keyof T]: UnwrapRef } +}[T extends ComputedRef + ? 'cRef' + : T extends Ref + ? 'ref' + : T extends Array + ? 'array' : T extends Function | CollectionTypes - ? T - : T extends object ? { [K in keyof T]: UnwrapRefPropValue } : T - -export type UnwrapRef = T extends Function | CollectionTypes - ? T - : T extends object - ? T extends Ref - ? { [K in keyof V]: UnwrapRefPropValue } - : { [K in keyof T]: UnwrapRefPropValue } - : T + ? 'ref' // bail out on types that shouldn't be unwrapped + : T extends object ? 'object' : 'ref']