From: Carlos Rodrigues Date: Fri, 8 Nov 2019 17:52:24 +0000 (+0000) Subject: types(reactivity): add support for tuples in ref unwrapping (#436) X-Git-Tag: v3.0.0-alpha.0~217 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=68ad302714715a44c0c616071852771267e8d913;p=thirdparty%2Fvuejs%2Fcore.git types(reactivity): add support for tuples in ref unwrapping (#436) --- diff --git a/packages/reactivity/__tests__/ref.spec.ts b/packages/reactivity/__tests__/ref.spec.ts index 34a440b139..8862f9a354 100644 --- a/packages/reactivity/__tests__/ref.spec.ts +++ b/packages/reactivity/__tests__/ref.spec.ts @@ -1,4 +1,4 @@ -import { ref, effect, reactive, isRef, toRefs } from '../src/index' +import { ref, effect, reactive, isRef, toRefs, Ref } from '../src/index' import { computed } from '@vue/runtime-dom' describe('reactivity/ref', () => { @@ -107,6 +107,27 @@ describe('reactivity/ref', () => { } }) + it('should keep tuple types', () => { + const tuple: [number, string, { a: number }, () => number, Ref] = [ + 0, + '1', + { a: 1 }, + () => 0, + ref(0) + ] + const tupleRef = ref(tuple) + + tupleRef.value[0]++ + expect(tupleRef.value[0]).toBe(1) + tupleRef.value[1] += '1' + expect(tupleRef.value[1]).toBe('11') + tupleRef.value[2].a++ + expect(tupleRef.value[2].a).toBe(2) + expect(tupleRef.value[3]()).toBe(0) + tupleRef.value[4]++ + expect(tupleRef.value[4]).toBe(1) + }) + 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 284bf6c1aa..19c042a0fe 100644 --- a/packages/reactivity/src/ref.ts +++ b/packages/reactivity/src/ref.ts @@ -72,11 +72,13 @@ function toProxyRef( } } +type UnwrapArray = { [P in keyof T]: 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 + array: T extends Array ? Array> & UnwrapArray : T object: { [K in keyof T]: UnwrapRef } }[T extends ComputedRef ? 'cRef'