]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(types): should unwrap array -> object -> ref
authorEvan You <yyx990803@gmail.com>
Mon, 29 Jun 2020 16:26:28 +0000 (12:26 -0400)
committerEvan You <yyx990803@gmail.com>
Mon, 29 Jun 2020 16:26:28 +0000 (12:26 -0400)
.eslintrc.js
packages/reactivity/src/ref.ts
test-dts/ref.test-d.ts

index 00c0019987eb6500f5550770acbf87d8a719da7a..cd2715b19af4498f56a22d0e68b9e9a3a5466fbe 100644 (file)
@@ -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'
index fe2b91c94f3e7f2ef01bf9569d55e19321e2a904..ee3fd4f5ed3f546fc999b192d1de91c681c55496 100644 (file)
@@ -165,10 +165,11 @@ type UnwrapRefSimple<T> = T extends
   | CollectionTypes
   | BaseTypes
   | Ref
-  | Array<any>
   | RefUnwrapBailTypes[keyof RefUnwrapBailTypes]
   ? T
-  : T extends object ? UnwrappedObject<T> : T
+  : T extends Array<any>
+    ? { [K in keyof T]: T[K] extends Ref ? T[K] : UnwrapRefSimple<T[K]> }
+    : T extends object ? UnwrappedObject<T> : T
 
 // Extract all known symbols from an object
 // when unwrapping Object the symbols are not `in keyof`, this should cover all the
index b08b1ca96cb8c90a83e98eaec103f70b42c59f78..4fb519a423b443ac654c5ea81aa2fbd388b1b8b8 100644 (file)
@@ -41,6 +41,23 @@ function plainType(arg: number | Ref<number>) {
   expectType<Ref<IteratorFoo | null | undefined>>(
     ref<IteratorFoo | null | undefined>()
   )
+
+  // should not unwrap ref inside arrays
+  const arr = ref([1, new Map<string, any>(), ref('1')]).value
+  const value = arr[0]
+  if (isRef(value)) {
+    expectType<Ref>(value)
+  } else if (typeof value === 'number') {
+    expectType<number>(value)
+  } else {
+    // should narrow down to Map type
+    // and not contain any Ref type
+    expectType<Map<string, any>>(value)
+  }
+
+  // should still unwrap in objects nested in arrays
+  const arr2 = ref([{ a: ref(1) }]).value
+  expectType<number>(arr2[0].a)
 }
 
 plainType(1)