]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(types): should unwrap tuple correctly (#3820)
authorHeYunfei <i.heyunfei@gmail.com>
Wed, 26 Oct 2022 08:27:42 +0000 (16:27 +0800)
committerGitHub <noreply@github.com>
Wed, 26 Oct 2022 08:27:42 +0000 (04:27 -0400)
fix #3819

packages/reactivity/src/ref.ts
test-dts/reactivity.test-d.ts

index 5843b3f63aeb4c69a7eebee9ea8b855540036268..2632160b31872201dd46ad80571af2bab4d86ae9 100644 (file)
@@ -280,13 +280,10 @@ export interface RefUnwrapBailTypes {}
 
 export type ShallowUnwrapRef<T> = {
   [K in keyof T]: T[K] extends Ref<infer V>
-    ? V
-    : // if `V` is `unknown` that means it does not extend `Ref` and is undefined
-    T[K] extends Ref<infer V> | undefined
-    ? unknown extends V
-      ? undefined
-      : V | undefined
-    : T[K]
+    ? V // if `V` is `unknown` that means it does not extend `Ref` and is undefined
+    : T[K] extends Ref<infer V> | undefined
+      ? unknown extends V ? undefined : V | undefined
+      : T[K]
 }
 
 export type UnwrapRef<T> = T extends ShallowRef<infer V>
@@ -303,7 +300,7 @@ export type UnwrapRefSimple<T> = T extends
   | RefUnwrapBailTypes[keyof RefUnwrapBailTypes]
   | { [RawSymbol]?: true }
   ? T
-  : T extends Array<any>
+  : T extends ReadonlyArray<any>
   ? { [K in keyof T]: UnwrapRefSimple<T[K]> }
   : T extends object & { [ShallowReactiveMarker]?: never }
   ? {
index 0499c6da2873f0fcd73a67b33d021e97b2a1c020..8722441ec14a540fa23df412f4df173a1014ed80 100644 (file)
@@ -1,62 +1,73 @@
-import {
-  ref,
-  readonly,
-  shallowReadonly,
-  describe,
-  expectError,
-  expectType,
-  Ref,
-  reactive,
-  markRaw
-} from './index'
-
-describe('should support DeepReadonly', () => {
-  const r = readonly({ obj: { k: 'v' } })
-  // @ts-expect-error
-  expectError((r.obj = {}))
-  // @ts-expect-error
-  expectError((r.obj.k = 'x'))
-})
-
-// #4180
-describe('readonly ref', () => {
-  const r = readonly(ref({ count: 1 }))
-  expectType<Ref>(r)
-})
-
-describe('should support markRaw', () => {
-  class Test<T> {
-    item = {} as Ref<T>
-  }
-  const test = new Test<number>()
-  const plain = {
-    ref: ref(1)
-  }
-
-  const r = reactive({
-    class: {
-      raw: markRaw(test),
-      reactive: test
-    },
-    plain: {
-      raw: markRaw(plain),
-      reactive: plain
-    }
-  })
-
-  expectType<Test<number>>(r.class.raw)
-  // @ts-expect-error it should unwrap
-  expectType<Test<number>>(r.class.reactive)
-
-  expectType<Ref<number>>(r.plain.raw.ref)
-  // @ts-expect-error it should unwrap
-  expectType<Ref<number>>(r.plain.reactive.ref)
-})
-
-describe('shallowReadonly ref unwrap', () => {
-  const r = shallowReadonly({ count: { n: ref(1) } })
-  // @ts-expect-error
-  r.count = 2
-  expectType<Ref>(r.count.n)
-  r.count.n.value = 123
-})
+import {\r
+  ref,\r
+  readonly,\r
+  shallowReadonly,\r
+  describe,\r
+  expectError,\r
+  expectType,\r
+  Ref,\r
+  reactive,\r
+  markRaw\r
+} from './index'\r
+\r
+describe('should support DeepReadonly', () => {\r
+  const r = readonly({ obj: { k: 'v' } })\r
+  // @ts-expect-error\r
+  expectError((r.obj = {}))\r
+  // @ts-expect-error\r
+  expectError((r.obj.k = 'x'))\r
+})\r
+\r
+// #4180\r
+describe('readonly ref', () => {\r
+  const r = readonly(ref({ count: 1 }))\r
+  expectType<Ref>(r)\r
+})\r
+\r
+describe('should support markRaw', () => {\r
+  class Test<T> {\r
+    item = {} as Ref<T>\r
+  }\r
+  const test = new Test<number>()\r
+  const plain = {\r
+    ref: ref(1)\r
+  }\r
+\r
+  const r = reactive({\r
+    class: {\r
+      raw: markRaw(test),\r
+      reactive: test\r
+    },\r
+    plain: {\r
+      raw: markRaw(plain),\r
+      reactive: plain\r
+    }\r
+  })\r
+\r
+  expectType<Test<number>>(r.class.raw)\r
+  // @ts-expect-error it should unwrap\r
+  expectType<Test<number>>(r.class.reactive)\r
+\r
+  expectType<Ref<number>>(r.plain.raw.ref)\r
+  // @ts-expect-error it should unwrap\r
+  expectType<Ref<number>>(r.plain.reactive.ref)\r
+})\r
+\r
+describe('shallowReadonly ref unwrap', () => {\r
+  const r = shallowReadonly({ count: { n: ref(1) } })\r
+  // @ts-expect-error\r
+  r.count = 2\r
+  expectType<Ref>(r.count.n)\r
+  r.count.n.value = 123\r
+})\r
+\r
+// #3819\r
+describe('should unwrap tuple correctly', () => {\r
+  const readonlyTuple = [ref(0)] as const\r
+  const reactiveReadonlyTuple = reactive(readonlyTuple)\r
+  expectType<Ref<number>>(reactiveReadonlyTuple[0])\r
+\r
+  const tuple: [Ref<number>] = [ref(0)]\r
+  const reactiveTuple = reactive(tuple)\r
+  expectType<Ref<number>>(reactiveTuple[0])\r
+})\r