From 82b28a5ecb95be1565e50427bfd5eefe4b2d408c Mon Sep 17 00:00:00 2001 From: Evan You Date: Mon, 29 Jun 2020 12:26:28 -0400 Subject: [PATCH] fix(types): should unwrap array -> object -> ref --- .eslintrc.js | 2 +- packages/reactivity/src/ref.ts | 5 +++-- test-dts/ref.test-d.ts | 17 +++++++++++++++++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 00c00199..cd2715b1 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -26,7 +26,7 @@ module.exports = { overrides: [ // tests, no restrictions (runs in Node / jest with jsdom) { - files: ['**/__tests__/**'], + files: ['**/__tests__/**', 'test-dts/**'], rules: { 'no-restricted-globals': 'off', 'no-restricted-syntax': 'off' diff --git a/packages/reactivity/src/ref.ts b/packages/reactivity/src/ref.ts index fe2b91c9..ee3fd4f5 100644 --- a/packages/reactivity/src/ref.ts +++ b/packages/reactivity/src/ref.ts @@ -165,10 +165,11 @@ type UnwrapRefSimple = T extends | CollectionTypes | BaseTypes | Ref - | Array | RefUnwrapBailTypes[keyof RefUnwrapBailTypes] ? T - : T extends object ? UnwrappedObject : T + : T extends Array + ? { [K in keyof T]: T[K] extends Ref ? T[K] : UnwrapRefSimple } + : T extends object ? UnwrappedObject : T // Extract all known symbols from an object // when unwrapping Object the symbols are not `in keyof`, this should cover all the diff --git a/test-dts/ref.test-d.ts b/test-dts/ref.test-d.ts index b08b1ca9..4fb519a4 100644 --- a/test-dts/ref.test-d.ts +++ b/test-dts/ref.test-d.ts @@ -41,6 +41,23 @@ function plainType(arg: number | Ref) { expectType>( ref() ) + + // should not unwrap ref inside arrays + const arr = ref([1, new Map(), ref('1')]).value + const value = arr[0] + if (isRef(value)) { + expectType(value) + } else if (typeof value === 'number') { + expectType(value) + } else { + // should narrow down to Map type + // and not contain any Ref type + expectType>(value) + } + + // should still unwrap in objects nested in arrays + const arr2 = ref([{ a: ref(1) }]).value + expectType(arr2[0].a) } plainType(1)