From 3cd2f7e68edd3db896e0254f3a4006798dcb0ef2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=A9=E6=95=A3=E6=80=A7=E7=99=BE=E4=B8=87=E7=94=9C?= =?UTF-8?q?=E9=9D=A2=E5=8C=85?= Date: Sat, 19 Oct 2019 02:54:05 +0800 Subject: [PATCH] types: fix ref unwrapping when nested inside arrays (#331) --- packages/reactivity/__tests__/ref.spec.ts | 19 +++++++++++++++++++ packages/reactivity/src/ref.ts | 5 ++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/packages/reactivity/__tests__/ref.spec.ts b/packages/reactivity/__tests__/ref.spec.ts index f2f02e10..89832ad6 100644 --- a/packages/reactivity/__tests__/ref.spec.ts +++ b/packages/reactivity/__tests__/ref.spec.ts @@ -80,6 +80,25 @@ describe('reactivity/ref', () => { expect(typeof (c.value.b + 1)).toBe('number') }) + it('should properly unwrap ref types nested inside arrays', () => { + const arr = ref([1, ref(1)]).value + // should unwrap to number[] + arr[0]++ + arr[1]++ + + const arr2 = ref([1, new Map(), ref('1')]).value + const value = arr2[0] + if (typeof value === 'string') { + value + 'foo' + } else if (typeof value === 'number') { + value + 1 + } else { + // should narrow down to Map type + // and not contain any Ref type + value.has('foo') + } + }) + test('isRef', () => { expect(isRef(ref(1))).toBe(true) expect(isRef(computed(() => 1))).toBe(true) diff --git a/packages/reactivity/src/ref.ts b/packages/reactivity/src/ref.ts index 24fabbc8..795be28b 100644 --- a/packages/reactivity/src/ref.ts +++ b/packages/reactivity/src/ref.ts @@ -74,7 +74,6 @@ export type UnwrapRef = { ref: T extends Ref ? UnwrapRef : T array: T extends Array ? Array> : T object: { [K in keyof T]: UnwrapRef } - stop: T }[T extends ComputedRef ? 'cRef' : T extends Ref @@ -82,5 +81,5 @@ export type UnwrapRef = { : T extends Array ? 'array' : T extends BailTypes - ? 'stop' // bail out on types that shouldn't be unwrapped - : T extends object ? 'object' : 'stop'] + ? 'ref' // bail out on types that shouldn't be unwrapped + : T extends object ? 'object' : 'ref']